1

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)

4
  • Who downvoted and asked for closing? This question is perfectly in the scope of SO. Commented Mar 2, 2017 at 10:00
  • @Jean-BaptisteYunès Obviously I didnt downvote, close request: but ... in the end, this question is really about basics. It doesn't surprise me that other users would rather go close this one. Commented Mar 2, 2017 at 11:31
  • I wasn't charging you for this... Even basics deserves to be answered, especially for beginners. Commented Mar 2, 2017 at 12:07
  • @Jean-BaptisteYunès I hope I didnt come over as defensive; I am just trying to explain things. And well, keep in mind: right on the very first page, it says Stack Overflow is a question and answer site for professional and enthusiast programmers. In contrast to popular believe, this is not a site for absolute newbies that are asking for absolute basics. And beyond that: one idea of voting is to express the level of "prior research" that went into a question. Long story short: things aren't that easy. Commented Mar 2, 2017 at 12:14

2 Answers 2

1

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"?!

Sign up to request clarification or add additional context in comments.

3 Comments

You may not be able to avoid it, if you use a library that needs a 2D array of strings then you may need to convert your own structure to what is requested. So your answer doesn't answers the original question.
I don't want OP to remove his acceptance, but your answer doesn't explain how convert list of list to 2D array.
@Jean-BaptisteYunès OK, added that part to my answer, too.
1

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]);

2 Comments

I am getting java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String
Yes needs slight modification in the conversions...done. Generic conversion should have been used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.