add a string to an 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); //this adds an element to the list.