115

I am new in Java so I need little help

I have

String [] scripts = new String [] ("test3","test4","test5");

and I want to add new strings ( string1,string2) to this array ( scripts) for examples

String string1= " test1"
String string2 = "test2"

I want to add the new string not in the init but in later phase

How I can do it ?

3
  • 2
    Can you use List (like ArrayList)? Or you can create a new array with bigger size, loop in old array add from index = 2. Then add new elements at index 0 and 1. Commented Dec 31, 2012 at 6:01
  • 1
    So you are not new to SO, do search before posting question to avoid downvote. Commented Dec 31, 2012 at 6:12
  • Go for java.util.Collections if you need elements to be modified dynamically. map the requirement and available Collections type. for above, as all suggested, ArrayList is preferable. Commented Dec 31, 2012 at 6:38

5 Answers 5

172

You cannot resize an array in java.

Once the size of array is declared, it remains fixed.

Instead you can use ArrayList that has dynamic size, meaning you don't need to worry about its size. If your array list is not big enough to accommodate new values then it will be resized automatically.

ArrayList<String> ar = new ArrayList<String>();
String s1 ="Test1";
String s2 ="Test2";
String s3 ="Test3";
ar.add(s1);
ar.add(s2);
ar.add(s3);

String s4 ="Test4";
ar.add(s4);
Sign up to request clarification or add additional context in comments.

1 Comment

Make sure to also import java.util.ArrayList
26

First, this code here,

string [] scripts = new String [] ("test3","test4","test5");

should be

String[] scripts = new String [] {"test3","test4","test5"};

Please read this tutorial on Arrays

Second,

Arrays are fixed size, so you can't add new Strings to above array. You may override existing values

scripts[0] = string1;

(or)

Create array with size then keep on adding elements till it is full.

If you want resizable arrays, consider using ArrayList.

Comments

10

you would have to write down some method to create a temporary array and then copy it like

public String[] increaseArray(String[] theArray, int increaseBy)  
{  
    int i = theArray.length;  
    int n = ++i;  
    String[] newArray = new String[n];  
    for(int cnt=0;cnt<theArray.length;cnt++)
    {  
        newArray[cnt] = theArray[cnt];  
    }  
    return newArray;  
}  

or The ArrayList would be helpful to resolve your problem.

3 Comments

+1 good. Also can use System.arrayCopy
You can use Arrays.copyOf() to achieve the same.
6

As many of the answer suggesting better solution is to use ArrayList. ArrayList size is not fixed and it is easily manageable.

It is resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

Note that this implementation is not synchronized.

ArrayList<String> scripts = new ArrayList<String>();
scripts.add("test1");
scripts.add("test2");
scripts.add("test3");

Comments

4

Since Java arrays hold a fixed number of values, you need to create a new array with a length of 5 in this case. A better solution would be to use an ArrayList and simply add strings to the array.

Example:

ArrayList<String> scripts = new ArrayList<String>();
scripts.add("test3");
scripts.add("test4");
scripts.add("test5");

// Then later you can add more Strings to the ArrayList
scripts.add("test1");
scripts.add("test2");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.