2D ArrayList Java示例
时间:2020-02-23 14:33:53 来源:igfitidea点击:
在本教程中,我们将看到如何在Java中创建2D ArrayList。
创建2D ArrayList的最佳方法是在Java中创建列表列表。
List<List> arraylist2D = new ArrayList<List>();
让我们创建一个程序来实现2d arraylist java。
package org.arpit.theitroad; import java.util.ArrayList; import java.util.List; public class Java2DArrayListMain { public static void main(String[] args) { //Intialize the arraylist List<List> arraylist2D = new ArrayList<List>(); //Create first list List list1=new ArrayList(); list1.add("List1_Str1"); list1.add("List1_Str2"); list1.add("List1_Str3"); //Create second list List list2=new ArrayList(); list2.add("List2_Str1"); list2.add("List2_Str2"); list2.add("List2_Str3"); //Create third list List list3=new ArrayList(); list3.add("List3_Str1"); list3.add("List3_Str2"); list3.add("List3_Str3"); list3.add("List3_Str4"); //Add individual list to arraylist2D arraylist2D.add(list1); arraylist2D.add(list2); arraylist2D.add(list3); //Let's retrieve element from the arraylist2D. System.out.println("2nd element in list3 : "+arraylist2D.get(2).get(1)); System.out.println("3nd element in list1 : "+arraylist2D.get(0).get(2)); System.out.println("1st element in list2 : "+arraylist2D.get(1).get(0)); } }
输出:
2nd element in list3 : List3_Str2 3nd element in list1 : List1_Str3 1st element in list2 : List2_Str1