Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Using Arraylist to store Matrix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Arraylist to store Matrix" Watch "Using Arraylist to store Matrix" New topic
Author

Using Arraylist to store Matrix

Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
I have a 3*3 matrix having the following values

A B C
A - 0.4 0.5
B 0.9 - 0.3
C 0.7 0.2 -

i want to store this values in an 2D ArrayList(ArrayList of ArrayList).I did the following

ArrayList probItem = new ArrayList();
ArrayList row = new ArrayList();

probItem.add("-");
probItem.add("0.4,");
probItem.add("0.5,");

row.add(probItem);

probItem = new ArrayList();
probItem.add("0.9,");
probItem.add("-");
probItem.add("0.3,");
row.add(probItem);

probItem = new ArrayList();
probItem.add("0.7,");
probItem.add("0.2,");
probItem.add("-");
row.add(probItem);

Is this the correct way ,can someone explain please.Thanks in advance
Bert Deville
Greenhorn

Joined: Oct 10, 2008
Posts: 8

This won't work, you always need to specify the class of the objects you want to store in your ArrayList, like ArrayList<Pizza>, ArrayList<String> or simply ArrayList<Object>.

Aside from the syntax error mentioned above, I see 2 problems here :
1) row.add(probItem) adds the reference probItem to the ArrayList, not the object itself. So you are adding 3 times the same reference.
2) probItem = new ArrayList() will create a new ArrayList and assign it to the probItem reference, the previous ArrayList referenced by probItem is now officialy lost.

In general, for this type of matrix with a fixed size, I think you would be better of using a simple 2D array instead of an ArrayList.
[ October 14, 2008: Message edited by: Bert Deville ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Originally posted by Bert Deville:
This won't work, you always need to specify the class of the objects you want to store in your ArrayList, like ArrayList<Pizza>, ArrayList<String> or simply ArrayList<Object>.

It will work; generics are not required. In Java 1.4 they aren't even allowed.
But I agree that using generics makes life easier in 99.99% of the time.

Aside from the syntax error mentioned above,

Not a syntax error
I see 2 problems here :
1) row.add(probItem) adds the reference probItem to the ArrayList, not the object itself. So you are adding 3 times the same reference.
2) probItem = new ArrayList() will create a new ArrayList and assign it to the probItem reference, the previous ArrayList referenced by probItem is now officialy lost.

I think you don't get it.

At first, a new ArrayList is created with one reference: probItem. Now probItem is added to row, and that ArrayList has two references: probItem and an element of row.

Next, a new ArrayList is created with one reference: probItem. The previous ArrayList still has a reference to it in row. The new ArrayList now has two references.

Remember, "probItem = new ArrayList()" puts a new value in probItem - a reference to the new ArrayList. So your 1) is incorrect - although probItem is added three times, it has different values at these times. And your 2) is incorrect because row still has a reference to all three ArrayLists.

In general, for this type of matrix with a fixed size, I think you would be better of using a simple 2D array instead of an ArrayList.

Well I can't disagree with you there.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Arraylist to store Matrix