2012-04-10 142 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)); 
     } 
    } 
    } 
}