Results 1 to 6 of 6
  1. #1
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default 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 :
    Java Code:
    String[][] s1 = new String[size][size];
    String[][] s2 = new String[size][size];
    ...
    ...
    Now, I create an ArrayList and store all these 2D String arrays in it :
    Java Code:
    ArrayList<String[][]> myList = new ArrayList<String[][]>();
    
    myList.add(s1);
    myList.add(s2);
    ...
    ...
    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.

    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!

  2. #2
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    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!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    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.

    You would extract the 2D array from the ArrayList and print it out as you would any other 2D array
    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).

    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!

  5. #5
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    hi Fubarable! I got it! Thanks for replying!

    Actually I misunderstood the concept of the enhanced for each loop.

    The loop must be :
    Java Code:
    for (String[][] a : myList)
    {
    
    }
    rather than :
    Java Code:
    for (ArrayList<String[][]> a : myList)
    {
    
    }
    I've just tried it out, it works. Thanks again!

    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!

  6. #6
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    You must spread some Reputation around before giving it to Fubarable again.

    hmmm
    Write a program to achieve anything you want in your life!

Similar Threads

  1. Replies: 12
    Last Post: 11-05-2009, 07:12 AM
  2. Arraylist with arrays?
    By Dieter in forum Advanced Java
    Replies: 13
    Last Post: 09-19-2009, 11:10 PM
  3. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  4. Accessing arrays in another class
    By rosh72851 in forum New To Java
    Replies: 6
    Last Post: 10-31-2008, 12:09 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •