简体   繁体   中英

How can I access the array(2-d) added to ArrayList?

How can I access the 2D arrays added to an ArrayList ?

import java.util.*;
class Input
{
   public static void main(String args[])
   {
       ArrayList arr=new ArrayList();
       int x;
       System.out.println("enter no of arrays");
       Scanner sc=new Scanner(System.in);
       x=sc.nextInt();
       int c=1;
       while(c<=x)
       {
         System.out.println("enter a case:");
         int[][] ch=new int[4][4];
         for(int i=0;i<4;i++)
         {
           System.out.println();
           for(int j=0;j<4;j++)
           {
             ch[i][j]=sc.nextInt();
           }
         }
         arr.add(ch);
         c++;
       }
   }
}

Parametrize the ArrayList :

ArrayList<int[][]> arr = new ArrayList<int[][]>();

Then you can simply access the elements as int[][] s via get() .

Specify a type for the ArrayList:

ArrayList<int[][]> arr;

Then arr.get(index) will correctly be treated as an int[][] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM