2012-04-10 58 views
0

我确实有这个代码,我想打印出Arraylist的所有数组值。 感谢您的高级帮助。在java中的arraylist阵列打印元素

这里是我的代码:

for (int i = 0; i <count; i++) { 
     System.out.println("list #" + i); 
     for (int j = 0; j < list[i].size(); j++) { 
      list[i].get(j); 

      System.out.println("elements of array in arraylist "+list[i].get(j)); 


     } 
    } 
+0

是...没有.... 12正常工作?又是什么问题?看起来你正在打印一些东西,这是错误的东西? – 2012-04-10 01:12:08

+0

什么是行列表[I] .get(j)为? – cloudygoose 2012-04-10 01:12:56

+0

yes.it是错误的。 – user1322978 2012-04-10 01:13:44

回答

5

对于存储在ArrayList中的数组的印刷元件,你将不得不做如下:

for each element of arraylist 
get array from arraylist 
    for each array element in array 
     print array element. 

你似乎是迭代List类型的数组。

编辑你的代码与数据结构

+0

thx伙计。它完美地工作 – user1322978 2012-04-10 01:37:55

4

见这是否可以为你工作。我认为这是简单的:

int numLists = 10; // Or whatever number you need it to be. 
ArrayList [] arrayOfLists = new ArrayList[numLists]; 
// you realize, of course, that you have to create and add those lists to the array. 
for (ArrayList list : arrayOfLists) { 
    System.out.println(list); 
} 

我不知道为什么你不喜欢列表的列表:

List<List<String>> listOfLists = new ArrayList<List<String>>(); 
// add some lists of Strings 
for (List<String> list : listOfLists) { 
    System.out.println(list); 
} 
4
for (Object[] array : list) 
    for (Object o : array) 
    System.out.println("item: " + o); 
0

下面的代码更详细,我

public class Solution 
{ 

    public static void main(String[] args) 
    { 

     int T,N,i,j,k=0,Element_to_be_added_to_the_array; 

     Scanner sn=new Scanner(System.in); 
     T=sn.nextInt(); 
     ArrayList<Integer>[] arr=new ArrayList[T]; 
     for(i=0;i<T;i++) 
     { 
      arr[k]=new ArrayList<Integer>(); 
      N=sn.nextInt(); 
      for(j=0;j<N;j++) 
      { 
       Element_to_be_added_to_the_array=sn.nextInt(); 
       arr[k].add(Element_to_be_added_to_the_array); 
      } 
      k++; 
     } 

//Printing elements of all the arrays contained within an arraylist 

    for(i=0;i<T;i++) 
    { 
     System.out.println("array["+i+"]"); 
     for(j=0;j<arr[i].size();j++) 
     { 
      System.out.println(arr[i].get(j)); 
     } 
    } 
    } 
}