简体   繁体   中英

How can i convert multi dimensional list to multidimensional array in java?

I have a List which hold an Array List and I want it to be converted into multidimensional array:

/*
The logs looks something like this
[Name: Foo, From: Somewhere, To: Somewhere]
[Name: Foo1, From: Somewhere1, To: Somewhere1]
[Name: Foo2, From: Somewhere2, To: Somewhere2]
...
*/

public static void main(String[] args) {
    List<ArrayList<String>> items = new ArrayList<>();

    for (String logs : Reader.read(App.purchaseLog, true) /* helper method */) {
    String[] singleLog = convert(logs); /* helper method */

    items.add(new ArrayList<String>());

    for (String data : singleLog) {
        String removeBrackets = data.replaceAll("\\[", "").replaceAll("\\]", "");

        String[] splitSingleLog = convert(removeBrackets.split(","));
        for (String element : splitSingleLog) {
            String trimmedData = element.trim();
            items.get(0).add(trimmedData.substring(trimmedData.indexOf(':') + 2, trimmedData.length()));
        }
    }
}

   // But then how can i convert it to normal 2 dimensional array?
   // i am expecting it to be like this
   String[][] data = {
       {"Foo", "Somewhere", "Somewhere"},
       {"Foo1", "Somewhere1", "Somewhere1"},
       {"Foo2", "Somewhere2", "Somewhere2"}
   };
}

public static String[] convert(String... array) {
    return array;
}

How can i loop each ArrayList to change it into normal array? I saw several examples on converting ArrayList into an array but it's mostly 1 dimensional array ( Convert ArrayList<String> to String[] array )

It seems that you don't understand how multi-dim arrays work in Java. They are not real matrices . An array of an array ... means: a 1-dim array that holds another 1-dim array.

Simply convert your List<Whatever> into a Whatever[] ; and then you can go and do something like:

Whatever[][] twodim = { row1, row2 };

or dynamically:

Whatever[][] twodim = new Whatever[numberOfRows][];
twodim[0] = row1;
twodim[1] = row2;
...

So, converting a List<List<String>> to a two-dim array works like:

List<List<String>> theStrings ... coming from somewhere
String[][] stringsAsArray = new String[theStrings.length][];
for (int i=0; i<theStrings.length;i++) {
  List<String> aList = theStrings.get(i);
  stringsAsArray[i] = aList.toArray(new String[aList.size()]);
}

But: why do you want to do that? As you just see yourself: those multi-dim arrays are complicated; and very inflexible compared to List. And looking at your data: your problem is actually that you are not using specific types.

In other words: if you have values that belong together conceptually, like some "item"; heck: then create an Item class that can hold values that belong together. So instead of dealing with List<List<String>> you are dealing with List<Item> instead!

That is how you should approach your problem: step back; and create a helpful object oriented model of your data. In essence, you should not be asking "how do I create multi-dim arrays of strings and stuff", but "how do I avoid exactly that"?!

You need two steps, first convert your List<List<X>> to List<X[]> and then to X[][] . Something like:

List<List<X>> listOfList = ...;

List<X[]> listOfArray a = new ArrayList<X[]>();
for (List<X> listOfXx : listOfList) listOfArray.add(listOfX.toArray(new X[0])); // fill with arrays (from inner lists)

X[][] arrayOfArray = listOfArray.toArray(new X[0][0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM