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);
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);
Arrays 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.
in java, once you declared an array you cannot change its size.
so that adding an element to the array is not possible.
you can declare an arraylist, which is the same as array but its resizable.
meaning that you can add or remove at any time and as much you want.
for example:
ArrayList<String> languages = new ArrayList<String>();
languages.add("PHP");
languages.add("JAVA");
languages.add("C#");
ArrayList<int> numbers = new ArrayList<int>();
numbers.add(9);
numbers.add(14);
numbers.add(2);