在java中打印arraylist中的数组元素
我有这段代码,我想打印出Arraylist的所有数组值。 感谢您在高级课程中的帮助 这是我的密码:在java中打印arraylist中的数组元素,java,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("element
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));
}
}
看看这是否对你有用。我认为这更简单:
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);
}
要打印存储在arraylist中的数组元素,必须执行以下操作:
for each element of arraylist
get array from arraylist
for each array element in array
print array element.
您似乎正在迭代列表类型的数组
编辑您的代码,并提供有关数据结构的详细信息下面的代码适合我
for (Object[] array : list)
for (Object o : array)
System.out.println("item: " + o);
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));
}
}
}
}
对不12? 问题是什么?看起来您正在打印一些内容,是不是打印错了?行列表[I].getj的作用是什么?我正在尝试获取值。但是,试图猜测数据结构的格式比用代码粘贴数据结构要困难得多,这是行不通的。