Two dimensional array list

前端 未结 9 1308
梦毁少年i
梦毁少年i 2020-11-28 05:09

I\'ve heard of using a two dimensional array like this :

String[][] strArr;

But is there any way of doing this with a list?

Maybe s

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 05:23

    Infact, 2 dimensional array is the list of list of X, where X is one of your data structures from typical ones to user-defined ones. As the following snapshot code, I added row by row into an array triangle. To create each row, I used the method add to add elements manually or the method asList to create a list from a band of data.

    package algorithms;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class RunDemo {
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // Get n
        List> triangle = new ArrayList>();
    
        List row1 = new ArrayList(1);
        row1.add(2);
        triangle.add(row1);
    
        List row2 = new ArrayList(2);
        row2.add(3);row2.add(4);
        triangle.add(row2);
    
        triangle.add(Arrays.asList(6,5,7));
        triangle.add(Arrays.asList(4,1,8,3));
    
        System.out.println("Size = "+ triangle.size());
        for (int i=0; i

    Running the sample, it generates the output:

    Size = 4
    [2]
    [3, 4]
    [6, 5, 7]
    [4, 1, 8, 3]
    

提交回复
热议问题