Results 1 to 6 of 6
- 01-17-2010, 08:06 PM #1
Accessing elements of an ArrayList which contains 2D arrays
Hi friends!
I have a problem which can be described as follows :
First, I have a list of 2D String arrays :
Now, I create an ArrayList and store all these 2D String arrays in it :Java Code:String[][] s1 = new String[size][size]; String[][] s2 = new String[size][size]; ... ...
Now What I want to do is to display contents of each 2D String arrays when I increment or decrement the index of the ArrayList myList.Java Code:ArrayList<String[][]> myList = new ArrayList<String[][]>(); myList.add(s1); myList.add(s2); ... ...
I was confused because the elements of the ArrayList are 2D arrays. Can you give me a solution for this or Is there any better way to store 2D arrays rather than using ArrayList.
Thanks in advance!Write a program to achieve anything you want in your life!
- 01-18-2010, 04:26 AM #2
I don't know if my question is too difficult or too easy to get some help. But at least I should get some replies please T_T
Write a program to achieve anything you want in your life!
-
You would extract the 2D array from the ArrayList and print it out as you would any other 2D array; there's no magic here. Perhaps you could use nested for loops or a for loop and inside it an Arrays.toString(...) call. But for me it's somewhat hard to answer you as we don't know what you've tried and how it doesn't work. You really should consider giving more information and at least showing us what you've tried first.
- 01-18-2010, 06:41 AM #4
Actually I am shooting in the dark.
The real situation is I am storing a list 2D String arrays in an ArrayList. Later I want to take the contents of each 2D array which is an element of the ArrayList and display them in a JTable. When the user clicks the "Next" button, then the contents of the JTable will be replaced by the contents of the 2D arrays in the next location within the ArrayList.
Your advice is definitely true, but as from above, I have only the index of the ArrayList to be used as the data to extract the 2D arrays from it. According to you, I should do something like this : (I know that to display the contents of a 2D array, we should use nested for loops, but in this situation, I don't know how to display the contents of a 2D array which is inside an ArrayList).You would extract the 2D array from the ArrayList and print it out as you would any other 2D array
Java Code:for (String[][] a : myList) { for (int i = 0; i < myList.size(); i++) { for (int j = 0; j < listSize; j++) // listSize is the size of each 2D array { // I am stucking here. // a.toString(); } } }Last edited by Willi; 01-18-2010 at 06:46 AM.
Write a program to achieve anything you want in your life!
- 01-18-2010, 06:52 AM #5
hi Fubarable! I got it! Thanks for replying!
Actually I misunderstood the concept of the enhanced for each loop.
The loop must be :
rather than :Java Code:for (String[][] a : myList) { }
I've just tried it out, it works. Thanks again!Java Code:for (ArrayList<String[][]> a : myList) { }
P/S Fubarable : Last time I did something like Reply Power for you, but actually I didn't understand much about reply powert, and I didn't know whether you got it or not. I think it's some kind of Thanks in other forum. Anyway, Rep again =^_^=Last edited by Willi; 01-18-2010 at 06:56 AM.
Write a program to achieve anything you want in your life!
- 01-18-2010, 07:00 AM #6
Similar Threads
-
Accessing an arrayList of multiple items per index.
By scrap in forum New To JavaReplies: 12Last Post: 11-05-2009, 07:12 AM -
Arraylist with arrays?
By Dieter in forum Advanced JavaReplies: 13Last Post: 09-19-2009, 11:10 PM -
Help printing specific ArrayList elements
By CirKuT in forum New To JavaReplies: 5Last Post: 02-03-2009, 12:24 AM -
Accessing arrays in another class
By rosh72851 in forum New To JavaReplies: 6Last Post: 10-31-2008, 12:09 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks