How to fill a two Dimensional ArrayList in java with Integers?

前端 未结 8 2460
迷失自我
迷失自我 2020-12-15 01:57

I have to create a 2d array with unknown size. So I have decided to go with a 2d ArrayList the problem is I\'m not sure how to initialize such an array or store information.

8条回答
  •  一个人的身影
    2020-12-15 02:08

      import java.util.*;
        public class ArrayListDS { 
            public static void main( String [] args ) { 
    
            ArrayList> row = new ArrayList>();
    
               Scanner sc = new Scanner(System.in);
    
               System.out.println("Enter the number of row: ");
               int n = sc.nextInt();
    
               for(int i = 0; i < n; i++){
    
                   ArrayList col = new ArrayList();
                   row.add(col);
    
               System.out.println("Enter the number of column: ");
               int m = sc.nextInt();
    
                   for(int j = 0; j < m; j++){
                        col.add(sc.nextInt()); 
                   }
                   System.out.println();
               }
               System.out.println(row);
            }
        }
    

提交回复
热议问题