0

I have a multidimentional array, as:

private static ArrayList [] [] pVTable = new ArrayList [35] [12];

My first try to initialize it was:

for (ArrayList[] x : pVTable) {
    for (ArrayList y : x) {
        y = new ArrayList<TableValue>();
    }
}

which didn't work.

I ended up doing it more manually, as in:

for ( int i = 0; i < pVTable.length; i++) {
    for ( int j = 0; j < pVTable[0].length; j++) {
        pVTable [i] [j] =  new ArrayList<TableValue>();
    }
}

which works fine.

Although I have a solution, I was wondering why the first (more elegant) piece of code doesn't do the same job?

2
  • that's because you are overwriting the object reference with a new ArrayList. Actually, y was anyways a null reference so initialization that ways would not have worked either ways. On a side note, Arrays.fill may help you Commented Nov 28, 2011 at 15:59
  • I haven't seen .fill before, I'll try to remember that. If I read it correctly, though, it would assign the SAME reference to all the table entries, not create a new ArrayList for each. Commented Nov 28, 2011 at 16:28

4 Answers 4

1

In the first snippet, if we strip away the syntactic sugar of the foreach operator (:), the code translates to:

for (int xIndex = 0; xIndex < pVTable.length; xIndex++) {
    ArrayList[] x = pVTable[xIndex];
    for (int yIndex = 0; yIndex < x.length; yIndex++) {
        ArrayList y = x[yIndex];
        y = new ArrayList<TableValue>();
    }
}

As you can see, nothing is ever assigned to the actual array – only to the temporary y variable.

1
  • When broken down this way, I can see what's going on. Thanks, Eli! Commented Nov 28, 2011 at 16:14
1

In the first example your code although modifies y does not change x.

1
  • Thanks for the reply, but I needed a little more explanation, as per Eli's comment. Commented Nov 28, 2011 at 16:17
1

You are mixing ArrayList (part of collections api) with Arrays, which is rather confusing (for me anyway)

I would suggest something like this instead :

List<Point> myShape =  new ArrayList<Point>;

Where point contains two ints representing X and Y.

3
  • Mmmhhh. Creating objects just to contain references to other objects sounds even more complicated to me, LOL. I guess I'm an old programmer still trying to program the old way! Commented Nov 28, 2011 at 16:21
  • @shakeshuck An arraylist is an object, as is TableValue. So you are doing this anyway. Commented Nov 28, 2011 at 16:24
  • Yes, but what I mean is that referencing row a, column b seems more straightforward than creating a new object to reference row a, column b. It feels like you're adding an extra element to do the same job? Like I said, maybe it's just an old brain's thinking... Commented Nov 28, 2011 at 16:40
0

The scope of the first is incorrect. y is just a placeholder variable. Changing that doesn't change the underlying object, just the object that y refers to. You can see the same problem in the following code snippet:

public static  int x = 2;

public static void foo(int y) {
    y = 3;//does nothing outside of foo
}

public static void main(String[] args) {
    System.out.println(x);//prints 2
    foo(x);
    System.out.println(x);//prints 2, x hasn't changed.
}

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.