Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arraylist and arrays

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I'm working with arraylists and was wondering how to convert the arraylist to a multi dimensional array. I am collection 12 monthly walues for differnt locations. In the past I had used:

rain [location][month]

Since I dont know the number of locations I'm adding each month to an arraylist. I might have 120 values and I would like to get them back in a for of rain [location] [month]. Any suggestions would be appreciated. Thanks...Ronnie
 
Thanks - I can get the arrayList to a single dimension array, but am having problems doing a 2 dimensional array. I tried an arrayList within an arrayList and it worked, but I could not get the data out. Thanks.
 
I'd go for an ArrayList containing 12-length array.

Cheers,

Dian
 
I was trying to figure this out the other day - I still haven't but the things I've read say that it is not possible - which I don't know if it's true or not.

But one suggestion I saw was to use a HashMap instead of an array list. I was going to explore this option - I just haven't yet.
 
Thanks, I'm using an ArrayList because I don't know the number of locations. I read that you can creat an Arraylist of an ArrayList, but the element is a a comma delimited Object that I'm having problems converting.
 
Maybe I am not understanding the problem, but does this example help?
Code:
import java.util.ArrayList;

public class TwoDimTest {
  public static void main(String[] args) {
    // Fill the ArrayLists with data and arbitrary sizes.
    ArrayList<ArrayList<String>> originalList = new ArrayList<ArrayList<String>>();
    for (int outerIdx = 0; outerIdx < 5; ++outerIdx) {
      originalList.add(new ArrayList<String>());
      for (int innerIdx = outerIdx; innerIdx >= 0; --innerIdx) {
        originalList.get(outerIdx).add(((Integer)innerIdx).toString());
      }
    }

    // Actually copy the arraylists to the 2-dimensional array.
    int size = originalList.size();
    String [][] twoDStringArray = new String[size][];
    for (int index = 0; index < size; ++index) {
      twoDStringArray[index] = originalList.get(index).toArray(new String[0]);
    }

    // Display to show the values in the arraylists.
    System.out.println("ArrayList Data:");
    for (ArrayList<String> innerArrList : originalList) {
      for (String value : innerArrList) {
        System.out.println(value);
      }
    }
    // Display to show the arrays contain the correct values.
    System.out.println("\nArray Data:");
    for (String[] outer : twoDStringArray) {
      for (String inner : outer) {
        System.out.println(inner);
      }
    }
  }
}
Focus on the second of the four blocks of code. This uses the toArray function that takes an argument to specify the proper type.
 
Thanks...I do appreciate your kind suggestions. I'm certain the snipet works. My problem is that I'm still using Java 1.4 and it does not support some of that code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top