What data structure does the following declaration specify?
List<ArrayList>[] myArray;
I think it should declare an array where each element is a List (e.g., a LinkedList or an ArrayList) and require that each List contain ArrayList objects.
My reasoning:
List<String> someList; // A List of String objects
List<ArrayList> someList; // A List of ArrayList objects
List<ArrayList>[] someListArray; // An array of List of ArrayList objects
After running some tests, I determined that it accepts an array where each element is an LinkedList object and does not specify what the LinkedList objects contain.
So List<ArrayList> specifies what the List must contain, but List<ArrayList>[] specifies how the List must be implemented.
Am I missing something?
Here are my tests.
import java.util.ArrayList; import java.util.List; import java.util.LinkedList; public class Generics1 { public static void main(String[] args) { List<ArrayList>[] someListArray; someListArray = getArrayWhereEachElementIsAnArrayListObject(); // Why does this satisfy the declaration? //someListArray[0] => ArrayList object holding Strings someListArray= getArrayWhereEachElementIsAListOfArrayListObjects(); //someListArray[0] => ArrayList object holding ArrayList objects } public static List[] getArrayWhereEachElementIsAnArrayListObject() { List[] arrayOfLists = new ArrayList[2]; arrayOfLists[0] = getStringList(); arrayOfLists[1] = getIntegerList(); return arrayOfLists; } public static List[] getArrayWhereEachElementIsAListOfArrayListObjects() { List list1 = new ArrayList(); list1.add(getArrayList()); List list2 = new ArrayList(); list2.add(getArrayList()); List[] arrayOfListsOfArrayLists = new ArrayList[2]; arrayOfListsOfArrayLists[0] = list1; arrayOfListsOfArrayLists[1] = list2; return arrayOfListsOfArrayLists; } public static List getStringList() { List stringList= new ArrayList(); stringList.add("one"); stringList.add("two"); return stringList; } public static List getIntegerList() { List intList= new ArrayList(); intList.add(new Integer(1)); intList.add(new Integer(2)); return intList; } public static ArrayList getArrayList() { ArrayList arrayList = new ArrayList() ; return arrayList; } }