0

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++;
       }
   }
}

2 Answers 2

3

Parametrize the ArrayList:

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

Specify a type for the ArrayList:

ArrayList<int[][]> arr;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.