0

I have a similar problem that is described below:

iterate static int values in java

The difference is that in my class I have elements that are ArrayLists (all ArrayLists contains elements of the same type), not primitive types.

I would like to know whether it is possible to iterate elements and inside this loop iterate declared ArrayList?

5
  • 1
    Can you give an example? Your question is a little vague. What do the ArrayLists contain ? Other reference types? Commented Dec 18, 2012 at 15:46
  • Yes, it is another reference type. Commented Dec 18, 2012 at 15:47
  • So you clarify: You have an ArrayList of some reference type A and you want to iterate over your ArrayList so you can access each of these A objects ? Commented Dec 18, 2012 at 15:48
  • I am sure the solution to original problem would be easier compared to problem itself. So key here is to understand what he wants :) Commented Dec 18, 2012 at 15:50
  • Yes, exactly. Similarly like in the link in the post, but instead of ints I have ArrayLists. And I would like to iterate over them. Commented Dec 18, 2012 at 15:50

2 Answers 2

2

You can directly iterate over ArrayLists, so your example is different from the link you posted, they needed to use reflection to iterate them because they weren't already grouped into a container:

class MyClass {
   public static ArrayList<A> myList = new ArrayList<>(
                                       Arrays.asList(new A[]{ A1, A2, ..., AN);

   public static void main(String[] args) {
       // iterate over your list
       for(A a : myList) {
          // do something with A object
       }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Indeed it looks like a sufficient solution. Anyway, now I will try to code this. Once again thanks a lot.
0

There are several ways to iterate over an ArrayList<Type> of a refrence type I found the following ways useful, you could try out yourself.

A. The most common/simple way of doing it, by using a counter to list its elements

    ArrayList<Person> persons = new ArrayList<Person>();
    //Add some person objects to the list
    for(int i=0;i<persons.size();i++){    
    System.out.println(persons.get(i));
    }

B. Using an iterator

   for (Iterator i = persons.iterator(); i.hasNext();) {
     Object listElement = i.next();
     System.out.println(listElement);
   }

C. A For/in loop

  for (Person listElement : persons) {
     System.out.println(listElement); 
   }

You could possibly use any of the 3 methods to get your work done, but it is recommended to use the last method which is the For/in loop since you won't have to use a counter (usually called i or count) or a Iterator

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.