Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a 2D String arraylist?

Tags:

I want to do something like this ArrayList<String<String>> mylist

How can I create it?

How can I add to the external and internal list

and how can I convert the internal list to a simple array list?

like image 509
code511788465541441 Avatar asked May 26 '12 16:05

code511788465541441


People also ask

How do you make a 2D ArrayList?

ArrayList<data_type> list_name = new ArrayList<>(int capacity); The above given is the syntax for array list creation in java, the array list needs to be created with the arraylist keyword as the first item. The array list forms the first item and then the data type of the array list needs to be declared.

How do you create a 2D String array in Java?

int columns = 2; int rows = 2; Here you are using the type String[][] to create a new 2D array with the size defined by [rows][columns] . String[][] newArray = new String[columns][rows]; You assign the values by its placement within the array.

Can we have 2D ArrayList?

Creating a multidimensional ArrayList often comes up during programming. In many cases, there is a need to create a two-dimensional ArrayList or a three-dimensional ArrayList.

How do you initialize a 2D String?

The quickest way I can think of is to use Arrays. fill(Object[], Object) like so, String[][] board1 = new String[10][10]; for (String[] row : board1) { Arrays. fill(row, "-"); } System.


1 Answers

You can go with

List<List<String>> ls2d = new ArrayList<List<String>>(); List<String> x = new ArrayList<String>(); x.add("Hello"); x.add("world!"); ls2d.add(x);  System.out.println(Arrays.deepToString(ls2d.toArray())); 
like image 175
Luiggi Mendoza Avatar answered Sep 29 '22 01:09

Luiggi Mendoza