Reputation: 1241
There's a way to iterate the member variables of an object in Java? I have this scenario:
class ItemToUpdateTipo0 {
public String ticker;
public ItemToUpdateTipo0() {
}
public ItemToUpdateTipo0(String ticker) {
this.ticker = ticker;
}
}
public class DataStoreTipo0 {
private Queue <ItemToUpdateTipo0> QueueItemToUpdateTipo0 = new ConcurrentLinkedQueue<ItemToUpdateTipo0>();
...
}
There's a way to do something like:
public static void main(String[] args){
DataStoreTipo0 dataStoreTipo0 = new DataStoreTipo0();
for (ItemToUpdateTipo0 obj : dataStoreTipo0) {
System.out.println(obj.titolo);
}
}
Thanks in advance.
Upvotes: 2
Views: 274
Reputation: 285450
Have your class implement the Iterable interface for this to work.
e.g.,
class DataStoreTipo0 implements Iterable<ItemToUpdateTipo0> {
private Queue<ItemToUpdateTipo0> queueItemToUpdateTipo0 = new ConcurrentLinkedQueue<ItemToUpdateTipo0>();
@Override
public Iterator<ItemToUpdateTipo0> iterator() {
return queueItemToUpdateTipo0.iterator();
}
}
Note that you'll want to edit your code so that it follows Java naming conventions. For instance classes should start with upper-case letters while methods and fields should start with lower-case letters. You'll also not want to try to directly access ItemToUpdateTipo0's fields but rather use public getter and possibly setter methods to do this for you.
Upvotes: 3