add string to array java code example
Example 1: java string array add element
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);
Example 2: how to add elements in string array in java
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);
Example 3: java add element to existing array
String[] rgb = new String[] {"red", "green"};
String[] rgb2 = new String[rgb.length + 1];
System.arraycopy(rgb, 0, rgb2, 0, rgb.length);
rgb2[rgb.length] = "blue";
rgb = rgb2;
Example 4: how to add objects in array java
car redCar = new Car("Red");
car Garage [] = new Car [100];
Garage[0] = redCar;