Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Generics Syntax for arrays
    primarykey
    data
    text
    <p>What data structure does the following declaration specify?</p> <pre><code> List&lt;ArrayList&gt;[] myArray; </code></pre> <p>I think it should declare an array where each element is a <code>List</code> (e.g., a <code>LinkedList</code> or an <code>ArrayList</code>) and require that each <code>List</code> contain <code>ArrayList</code> objects.</p> <p>My reasoning:</p> <pre><code> List&lt;String&gt; someList; // A List of String objects List&lt;ArrayList&gt; someList; // A List of ArrayList objects List&lt;ArrayList&gt;[] someListArray; // An array of List of ArrayList objects </code></pre> <p>After running some tests, I determined that it accepts an array where each element is an <code>LinkedList</code> object and does not specify what the LinkedList objects contain.</p> <p>So <code>List&lt;ArrayList&gt;</code> specifies what the <code>List</code> must contain, but <code>List&lt;ArrayList&gt;[]</code> specifies how the <code>List</code> must be implemented.</p> <p>Am I missing something?</p> <p>Here are my tests.</p> <pre><code>import java.util.ArrayList; import java.util.List; import java.util.LinkedList; public class Generics1 { public static void main(String[] args) { List&lt;ArrayList&gt;[] someListArray; someListArray = getArrayWhereEachElementIsAnArrayListObject(); // Why does this satisfy the declaration? //someListArray[0] =&gt; ArrayList object holding Strings someListArray= getArrayWhereEachElementIsAListOfArrayListObjects(); //someListArray[0] =&gt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload