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
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);