Array's in Java are not dynamic. In other words, they have a fixed sized in memory, and you can add/remove items from the spots allocated to it in memory.
You can either create an array of a particular size:
public String[] testArray = new String[5]; // array of size 5 in memory
Now you can add into the 0
th index of your array as there is "room" for it.
Alternatively, you can use an ArrayList
, which is a dynamic ADT (ie it can grow in size).
public ArrayList<String> myList = new ArrayList<String>();
And then you can append items to it using .add(String item)
:
if(testvalue == 1) {
myList.add("value1");
}
However, if you are doing to use an ArrayList you need to make sure you import the appropriate libraries:
import java.util.ArrayList;