Creating a dynamic 2D matrix in Java

后端 未结 1 362
你的背包
你的背包 2021-01-29 03:38

I want a dynamic matrix, number rows and columns unkonw, filling it by clicking on a button. Bu there is more: I don\'t want to add entire rows, but just one cell at the time, o

相关标签:
1条回答
  • 2021-01-29 04:03

    Use this:

    List<List<Integer>> dynamicMatrix = new ArrayList<List<Integer>>();
    
    dynamicMatrix.add(new ArrayList<Integer>());
    dynamicMatrix.add(new ArrayList<Integer>());
    dynamicMatrix.add(new ArrayList<Integer>());
    
    dynamicMatrix.get(0).add(6);
    dynamicMatrix.get(0).add(7);
    dynamicMatrix.get(0).add(8);
    
    System.out.println(dynamicMatrix.get(0).get(0)); // 6
    System.out.println(dynamicMatrix.get(0).get(1)); // 7
    System.out.println(dynamicMatrix.get(0).get(2)); // 8
    
    0 讨论(0)
提交回复
热议问题