-1

I am getting an exception in my Java program regarding my array list of array lists. I've included the relevant part of my program below.

List<List<Integer>> inverse_men_preference = new ArrayList<List<Integer>>(n);
    for (int i = 0; i < n; i++)
    {
        inverse_men_preference.add(new ArrayList<Integer>(n));
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            inverse_men_preference.get(i).add(marriage.getMenPreference().get(i).get(j), j);
        }
    }

I looked at similar questions, but I still could not figure out why my array list's size is considered empty. I am new to Java, so I figured I had some fundamental misunderstanding preventing me from understanding the error. Any input or suggestions would be helpful. Thank you.

Gopika
  • 35
  • 1
  • 6
  • 3
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) –  Sep 25 '16 at 19:30

3 Answers3

4

You've put no items in the ArrayList. the n is simply its capacity, not its size. Always use a collection's size() and avoid using magic numbers as you're doing here.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks. Why wouldn't the first for loop in which I add ten array lists in the array list not make the size ten? I hope my question makes sense. – Gopika Sep 25 '16 at 19:34
  • @Gopika He is obviously talking about the ArrayList you've added to `inverse_men_preference`. They are empty. – Tom Sep 25 '16 at 20:21
  • 1
    @Tom Thank you for the clarification! I guess I automatically assumed the problem was with the ArrayList inverse_men_preference. – Gopika Sep 25 '16 at 20:47
0

Looks like the error you are getting is because of marriage.getMenPreference().get(i).get(j) part. Maybe either of the lists returned by marriage.getMenPreference() does not have ith or jth element. If we replace that with constant, it works fine, e.g.:

int n = 2;
List<List<Integer>> inverse_men_preference = new ArrayList<List<Integer>>(n);
for (int i = 0; i < n; i++) {
    inverse_men_preference.add(new ArrayList<Integer>(n));
}

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        inverse_men_preference.get(i).add(0, j);
    }
}

Make sure you have marriage.getMenPreference() initialised/populated before the above gets executed.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Thanks for the reply! This is not the issue as these are passed into the function already populated. – Gopika Sep 25 '16 at 20:48
0

It is not the problem with any of your lists. The exception is appointing while instead of adding an single integer - you call method "add" with two arguments. This works good:

public static void main(String[] args) {
    int n = 10;
    List<List<Integer>> inverse_men_preference = new ArrayList<List<Integer>>(n);

    for (int i = 0; i < n; i++)
        inverse_men_preference.add(new ArrayList<Integer>(n));

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            inverse_men_preference.get(i).add(5);
}

whereas this is what you do:

public static void main(String[] args) {
    int n = 10;
    List<List<Integer>> inverse_men_preference = new ArrayList<List<Integer>>(n);

    for (int i = 0; i < n; i++)
        inverse_men_preference.add(new ArrayList<Integer>(n));

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            inverse_men_preference.get(i).add(5, 34);
}

List is such a collection you can insert elements to the beginning / in the middle / to the end of it. While it is empty you cannot add element e.g. on the fifth position. What I would recommend is using arrays instead of arraylist, hence you will be able to insert values in different positions of it at any time.

Wojtek
  • 1,288
  • 11
  • 16
  • Thanks for your answer. There is an add method that takes two arguments--the index and the element to be added to that index. So why wouldn't my approach work? – Gopika Sep 25 '16 at 19:47
  • @Gopika I have edited my answer to explain your exception. – Wojtek Sep 25 '16 at 19:58
  • Thank you for your reply! I used an array list of arrays like you recommended. This got rid of my initial error since I don't have the empty array list problem. However, it still causes an error later because I get a null pointer exception since the arrays in the arraylist have not been initialized. Which is essentially the same as my earlier problem. Also, I realized I need to use an array list rather than an array anyway because I want to make use of the shifting property of the add method when two objects map to the same index. – Gopika Sep 25 '16 at 20:43
  • @Gopika try this: http://pastebin.com/3inVAxDB and tell if it worked. It would be really cool if you would accept my answer as the correct one, cheers! – Wojtek Sep 25 '16 at 20:48