1ArrayList<String> ar = new ArrayList<String>();
2String s1 ="Test1";
3String s2 ="Test2";
4String s3 ="Test3";
5ar.add(s1);
6ar.add(s2);
7ar.add(s3);
8
9String s4 ="Test4";
10ar.add(s4);
11
1Arrays in Java have a defined size, you cannot change it later by adding or removing elements (you can read some basics here). Instead, use a List : ArrayList<String> mylist = new ArrayList<String>(); mylist. add(mystring); //this adds an element to the list.
2
1// A better solution would be to use an ArrayList which can grow as you need it.
2// The method ArrayList.toArray( T[] a )
3// gives you back your array if you need it in this form.
4
5List<String> where = new ArrayList<String>();
6where.add(element);
7where.add(element);
8
9// If you need to convert it to a simple array...
10
11String[] simpleArray = new String[ where.size() ];
12where.toArray( simpleArray );
1ArrayList<String> ar = new ArrayList<String>();
2String s1 ="Test1";
3String s2 ="Test2";
4String s3 ="Test3";
5ar.add(s1);
6ar.add(s2);
7ar.add(s3);
8
9String s4 ="Test4";
10ar.add(s4);
1in java, once you declared an array you cannot change its size.
2so that adding an element to the array is not possible.
3you can declare an arraylist, which is the same as array but its resizable.
4meaning that you can add or remove at any time and as much you want.
5for example:
6ArrayList<String> languages = new ArrayList<String>();
7languages.add("PHP");
8languages.add("JAVA");
9languages.add("C#");
10ArrayList<int> numbers = new ArrayList<int>();
11numbers.add(9);
12numbers.add(14);
13numbers.add(2);