0

How to create array or list of Lists in Java?

I have:

List<Item> rows = new ArrayList<Item>();

rows.add(new Item("a"));
rows.add(new Item("b"));

... and how to create array of lists like this above?

List<Item> lists[] = {};
lists[0] = rows;

Here is what I am trying to do:

public class MainActivity extends FragmentActivity {

    public static List<List<Item>> lists;
    String tabs[] = { "Tab 1", "Tab 2" };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lists = new ArrayList<List<Item>>();

        for (int i = 0; i < tabs.length; i++)
        {
            lists.add(i, init());
        }
    }

    public List<Item> init()
    {
        List<Item> rows = new ArrayList<Item>();

        rows.add(new Item("test"));

        return rows;
    }

}

... and then I am trying to get that list by using:

MainActivity.lists.get(0);
1
  • You should initialize the static lists in static block. Commented Aug 1, 2013 at 16:21

2 Answers 2

4

You should not create an array of generics types, because it's not type safe. Here's a quote from that link

Parameterized types do not have exact runtime type information. As a consequence, the array store check does not work because it uses the dynamic type information regarding the array's (non-exact) component type for the array store check.

Emphasis mine.

Basically, array uses a store check at runtime, to check whether the elements stored in it, is compatible with the type of actual array. Now, since a generic type doesn't have actual type information at runtime due to type erasure, the array store check won't work.

E.g. (Taken from the above link):

Pair<Integer,Integer>[] intPairArr = new Pair<Integer,Integer>[10]; // illegal
Object[] objArr = intPairArr; 
objArr[0] = new Pair<String,String>("",""); // should fail, but would succeed

It would work because Pair<Integer, Integer>[], is actually Pair[] at runtime, and will successfully store Pair<String, String>, which is also a Pair at runtime.

But that would fail further in your code, once you try to fetch elements from array.


So, you should rather create a list of list:

List<List<Item>> itemList = new ArrayList<List<Item>>();
itemList.add(rows);
Sign up to request clarification or add additional context in comments.

8 Comments

I am creating an app for Android, and I want to use this variable globaly, as a static variable. When variable is set, I want to get one list by using .get() method, but my app is crashing. Could you provide example with setting static variable using list of list, and then adding a example list?
@Damian. How are you getting the list? Can you post your code in your question?
Okey, updated, code is compiling, but when I launch the app it is crashing.
@Damian. See my comment on your question. Use a static block to initialize the list. This will ensure that the list is not null when loaded.
Like this? static { lists = new ArrayList<List<Item>>(); }
|
2

This is how you can create List of List

List<List<Item>> listOfList = new ArrayList<List<Item>>();
listOfList.add(new ArrayList<Item>());
listOfList.get(0).add(new Item());

and List of array

List<Item[]> listOfArray = new ArrayList<Item[]>();
listOfArray.add(new Item[] { item1, item2});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.