2

The situation looks like this:

First class:

class Something {
    List<Object> names;
    public Something(){
       this.names = new ArrayList<Object>();    
    }
}

Second class I tried do something like that and it is not working:

class Main {
    public static void main(String[] args){
        Something names = new Something();
        ...
        for(Object name: names){
           ...
        }
    }
}

And it says that I can't iterate through "names" why? What am I doing wrong?

2
  • 5
    Something does not implement Iterable<T>, so you cannot iterate over it directly; you must iterate over its names field: for(Object name : names.names) Commented Aug 25, 2018 at 16:55
  • 1
    Getter/setter ... better yet, let your IDE create them for you, so you may avoid carpal tunnel. Commented Aug 25, 2018 at 17:00

4 Answers 4

2

You probably intend to iterate over the list inside your Something class:

public static void main(String[] args){
    Something something = new Something();
    // then assign something.names to something
    ...
    for (Object name: something.getNames()) {
        // do something
    }
}

So, you probably want to expose a getter and setter for the names list in your class:

class Something {
    List<Object> names;

    public Something() {
        this.names = new ArrayList<Object>();    
    }

    public List<Object> getNames() {
        return names;
    }

    public void setNames(List<Object> names) {
        this.names = names;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

The other answers give fine solutions. But what if you do want to use just something in the enhanced for loop?

You have to make the class implement Iterable:

class Something implements Iterable<Object> {

And implement the iterator() method:

@Override public Iterator<Object> iterator() {
  return names.iterator(); // for example.
}

Now you can write:

for (Object object : something) { ... }

and it will be acceptable to the compiler.

But you should only do this if Something is-a Iterable of Objects, i.e. that it is intrinsically modelling something which should be iterated (e.g. it is "like a List"). Otherwise, if it merely has-a Iterable of Objects, stick to the other proposed solutions.

Comments

1

Try not to call your Something instance names. names is a member of the Something class instance. You are trying to iterate your Something class, and not names array inside it.

class Main {
    public static void main(String[] args){
        Something something = new Something();
        ...
        for(Object name: something.names){
           do smth;
        }
    }
}

Comments

1

Your Something class is not a List. But it contains a List.

class Something {
    List<Object> names;

    public List<Object> getNames(){
       this.names = new ArrayList<Object>();    
    }

    public void setNames( List<Object> list ){
       this.names = list;    
    }
}

class Main {
    public static void main(String[] args){
        Something somethingObj= new Something();
       List<Object> newNames = new ArrayList<>();
       newNames.add( new Object() );
       somethingObj.setNames( newNames ); 

        for(Object name: somethingObj.getNames()){
           ...
        }
    }
}

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.