-2

So basically I have an array I create in the start of my script,

public String[] testarray;

Then I later in the script I want to add things to the array:

if(testvalue == 1) {
  testarray[0] = "value1";
}

This obviously doesn't work. How would I do this?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 2
    You will need to initialize the array with size. If this is not possible then use a `List` – Sid Feb 09 '19 at 04:20
  • Your array dont have a size?? – Vishwa Ratna Feb 09 '19 at 04:24
  • Exactly as @Sid points out. If you know your array will have a fixed length, even if that length is computed at runtime, you can stick to `String[]`. When dealing with arrays whose length are dynamic, `List` (likely implemented as `ArrayList`) will be the easier option. – vapurrmaid Feb 09 '19 at 04:25

3 Answers3

0

In Java, Arrays have to be initialized with size at compile time. The size may be a variable, but has to be deterministic when the code runs. Hence you cannot just dynamic sized arrays.

So the following would work:

public String[] testarray = new String[n]; // n is an int

if(testvalue == 1) {
  testarray[0] = "value1"; // assuming n>0
}

If you absolutely need dynamic sized arrays, the ArrayList will work for you.

ArrayList<String> testList = new ArrayList<String>();
testList.add("value1");

Lists are basically arrays that expand on demand.

Sid
  • 4,893
  • 14
  • 55
  • 110
0

public String[] testarray; // This array needs to be initialised of some size.

However, if you do not have any prediction about the length of data you need to store, you can use dynamic collections API like:-

public List<String> testList = new ArrayLisy<String>();
0

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 0th 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;
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 2 Questions: First, when I use myList.add("value1"); it says Cannot invoke add(String) on the array type String[] And second, if I don't use all the size in the array is that a problem? – user3125845 Feb 09 '19 at 04:46
  • @user3125845 is `myList` an `ArrayList` ? – Nick Parsons Feb 09 '19 at 04:51
  • I realized it wasn't and fixed it. So if my array is say 5 long but I only put 2 strings into it is that a problem? – user3125845 Feb 09 '19 at 04:57
  • If you did that you would be waisting memory space, but it's not the end of the world if you did that. But if you're only going to put 2 strings into it then you should create an array of 2 long – Nick Parsons Feb 09 '19 at 05:00