user user - 4 months ago 8
Java Question

What is the type String[]?

I came across code that declared the following

List <String[]> l1 = new ArrayList<String[]>();


I'm unsure what this is a list of. How would you add an instance of String[] to the list l1?

Answer
List <String[]> l1 = new ArrayList<String[]>();

Is a list of arrays of strings. Could be considered equivalent to String[][] except the list can be added to and removed from like this:

String[] myStringArray = new String[]{ "1","2","3"};
List<String[]> myListOfStringArrays = new List<>();
myListOfStringArrays.Add(myStringArray);
myListOfStringArrays.Remove(myStringArray);

You can even make it a String[][] by calling myListOfStringArrays.ToArray();

Comments