0

I have a 2-dimensional array and I want to convert it into a Arraylist. How should I organize such list?

For instance when I have array

String[][] things={{"dog", "cat", "wolf"},
                   {"carrot", "apple", "banana"}};

I would like to have list

List<what is proper type?> Galg = ...

which will allow me to get row indexed as x and get item from that row with index y?

17
  • 3
    The question does not make sense because the first type has 2-dimensions, and the second type has 3-dimensions. Also .. include a language tag and remove any irrelevant tags. Commented Jul 25, 2015 at 15:45
  • Maybe the question does not makes sence cause i am doing things wrong. Perhaps this is also why i ask the question. but thx anyway. Commented Jul 25, 2015 at 15:53
  • Can you give example of content of that list and how would you like to use it? Commented Jul 25, 2015 at 15:54
  • For now I am voting to put your question on hold because it is unclear what you want to achieve. I will retract my vote or vote to reopen this question when you provide more informations about how would you like to use that list and what data should it contain. Commented Jul 25, 2015 at 15:58
  • What is not clear? i just want an Arraylist containing.{"dog", "cat", "wolf"} and { "carrot", "apple", "banana"} . Why is this not clear i dont understand? Commented Jul 25, 2015 at 16:03

2 Answers 2

1

It looks like you are looking for ArrayList<ArrayList<String>> or better lets program on interfaces rather than actual type and make our reference more general (which also means flexible because it can handle many lists, not only ArrayList) with

List<List<String>> list = new ArrayList<List<String>>();

or since Java 7 we can shorten it using diamond operator and let generic type be inferred

List<List<String>> list = new ArrayList<>();

You can create it via code like

String[][] things={{"dog", "cat", "wolf"},{ "carrot", "apple", "banana"}};
List<List<String>> list = new ArrayList<>();
for (String[] row : things){
    list.add(new ArrayList<>(Arrays.asList(row)));
}

and use it like

System.out.println(list.get(1).get(2));//get row indexed as 1, 
                                       //and from it item indexed as 2

Try to think of list as one dimensional array. Actually all arrays are one dimensional, because two dimensional arrays are simply one dimensional arrays which elements are other one dimensional arrays (you are nesting one dimensional arrays).

So your array

A{     
  [0] -> B{ 
           [0] -> "dog", 
           [1] -> "cat", 
           [2] -> "wolf"
         }
  [1] -> C{ 
           [0] -> "carrot", 
           [1] -> "apple", 
           [2] -> "banana"
         }
}

So we have one dimensional array A which contains other one dimensional arrays B and C.

Same is possible for list. You can nest them just like arrays are nested.

ListA{     
      [0] -> ListB{ 
                   [0] -> "dog", 
                   [1] -> "cat", 
                   [2] -> "wolf"
             }
      [1] -> ListC{ 
                   [0] -> "carrot", 
                   [1] -> "apple", 
                   [2] -> "banana"
             }
}

So we can have List< of Lists< of String>> which is written as List<List<String>

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

Comments

1

Currently, you can just do

Galg.add(things);

But, perhaps you need an ArrayList<String[]> instead? In that case, you can use Arrays.asList:

ArrayList<String[]> Galg = new ArrayList<String[]>();
Galg.addAll(Arrays.asList(things));

2 Comments

I want the Arraylist to be exactly the same as the array, could you give me a example please?
Then, use the second example. You can add more items to the list later, for instance with Galg.add(new string[] { "a", "b", "c" });.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.