2013-08-08 2 views
0

Как я могу получить доступ к 2D-массивам, добавленным к ArrayList?Как получить доступ к массиву (2-d), добавленному в 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++; 
     } 
    } 
} 

ответ

1

Укажите тип для ArrayList:

ArrayList<int[][]> arr; 

Тогда arr.get(index) правильно будет рассматриваться как int[][].

3

параметризующих ArrayList:

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

Тогда вы можете просто получить доступ к элементам как int[][] с помощью get().