簡體   English   中英

如何訪問添加到ArrayList的array(2-d)?

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

如何訪問添加到ArrayList的2D數組?

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

參數化ArrayList

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

然后,您可以通過get()int[][]的形式訪問元素。

指定ArrayList的類型:

ArrayList<int[][]> arr;

然后arr.get(index)將被正確地視為int[][]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM