0

I do have this code, and I would like to print out all the array's values of Arraylist. thanks for your help in advanced.

here is my code:

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));


        }
    }
4
  • Yes... no.... 12? What was the question again? It looks like you are printing out some stuff, is it the wrong stuff? Commented Apr 10, 2012 at 1:12
  • What is the line list[I].get(j) for? Commented Apr 10, 2012 at 1:12
  • I was trying to get the values. but it is not working well Commented Apr 10, 2012 at 1:14
  • 3
    Trying to guess the format of your data structure is a lot harder than you pasting it with your code. Commented Apr 10, 2012 at 1:15

4 Answers 4

5

For printing elements of an array stored in arraylist,you will have to to do the following:

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

You seemed to be iterating array of List type instead.

Edit your code with further detail on your data structure

Sign up to request clarification or add additional context in comments.

Comments

4
for (Object[] array : list)
  for (Object o : array)
    System.out.println("item: " + o);

Comments

4

See if this can work for you. I think it's simpler:

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

I'd wonder why you don't prefer a List of Lists:

List<List<String>> listOfLists = new ArrayList<List<String>>();
// add some lists of Strings
for (List<String> list : listOfLists) {
    System.out.println(list);
}

Comments

0

Below code works fine for me

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.