Studyon Minte9.com
Java SE




List (add, indexOf)



The size of array can't be modified

String str[] = new String[3]{};
A better solution is to use ArrayList
package testexample; import java.util.ArrayList; import java.util.Arrays; public class Test { public static void main(String[] args) { // --- Init --- // ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); for (String s : list) { System.out.println(s); // Output: a, b } System.out.println(); // --- Init (second form) --- // ArrayList<String> list2 = new ArrayList<String>(){{ add("c"); add("d"); }}; for (String s : list2) { System.out.println(s); // Output: c, d } System.out.println(); // --- Arrays.asList / indexOf --- // ArrayList<String> list3 = new ArrayList<String>(Arrays.asList("a", "b")); list3.add("c"); // add new for (String s : list3) { if(list3.indexOf(s) == list3.size()-1) { System.out.println(s); // Output: c } } } }
 


http://stackoverflow.com/questions/2843366/how-to-add-new-elements-to-an-array
http://www.mkyong.com/java/how-to-loop-arraylist-in-java/