The following code does not work. What is wrong with this code? The compiler complains in a for loop that NumberList not an Iterable class.
Which class can be used for each cycle? How to make NumberList iterable? I tried to make NumberList implement Iterable , but it does not work, because I do not know how to correctly determine Iterator.
If someone can demonstrate how to make this code work, or link me to a tutorial, that would be great.
public class Test{ public class NumberList{ private int numItems; private Number[] numbers; public NumberList(int size){ this.numbers = new Number[size]; this.numItems=0; } public void add(Number n){ this.numbers[this.numItems++]=n; } } public void printPairs() { ArrayList<Integer> num=new ArrayList<Integer>(); NumberList numbers = new NumberList(50); numbers.add(4); numbers.add(5); numbers.add(6); for(Number n1: numbers){ System.out.println(n1); } } }
source share