Faced with such a problem as getting data from a sheet. It would seem that everything is simple, but not quite. Look here:

ArrayList<String> a = new ArrayList<>(); a.add("1"); a.add("2"); a.add("3"); ArrayList<String> b = new ArrayList<>(); b.add(a.toString()); String splitStr = b.get(0); System.out.println(splitStr); 

I get this kind of data:

 [1, 2, 3] 

So the actual question: How to get access to these elements? The result was a two-dimensional array. But I do not understand how to get to him? I tried this:

 for(String getSplit : splitStr.split("([),(])")){System.out.println("str "+getSplit);} 

But square brackets remain.

 str [1 str 2 str 3] 

Ie doing something wrong. Tried through regex

  Pattern pat = Pattern.compile("([),(])"); Matcher mat = pat.matcher(splitStr); while(mat.find()) { System.out.println("str "+mat.group()); } 

I get commas. Are there any standard methods? Not to reinvent the wheel.

In general, it turned out like this:

  ArrayList<String> a = new ArrayList<>(); a.add("1"); a.add("2"); a.add("3"); ArrayList<ArrayList> b = new ArrayList<>(); b.add(a); for(int q =0; q < b.size(); q++ ) { for(int t=0; t< a.size(); t++) { System.out.println( b.get(q).get(t)); } } 

Thank you all for your attention.

  • 2
    Either I did not understand you, or you are very confused. b is not a two-dimensional array, it is a list of strings. The fact that you write there the value of toString in another list does not make it a two-dimensional array. - Vartlok
  • How to make an ArrayList<ArrayList<String>> - Bohdan Korinnyi
  • one
    If you have found a solution yourself, then publish it in the form of an answer (the button “Answer your own question”), and do not give an answer in the question. The title of the question should contain a brief essence of the problem, but not artistic delights. - pavlofff

1 answer 1

Maybe here you will use this scheme?

 ArrayList<ArrayList<String>> arr2 = new ArrayList<>(); // один массив в другом массиве. ArrayList<String> arr1 = new ArrayList<>(); arr1.add("1"); arr1.add("2"); arr1.add("3"); arr2.add(arr1); System.out.println(arr2.get(0).toString()); // будет [1, 2, 3] for (String s : arr2.get(0)) { System.out.println(s); } 

Conclusion

 [1, 2, 3] 1 2 3 

The square brackets remain, because the elements of the list are represented in this form and not only. ClassName[element1 = value1, element2 = value2] or ClassName[value1,value2] usually used if the class has one data type, as in your case.