this is my first question here. I have played around a little and created a class named MyIterator which implements the interface Iterator. Everything works great when I use it with the following array :
String u [] = {"Hello", "Whats", "Up"};
MyIterator <String> strins = new MyIterator <> (u);
while(strins.hasNext()) {
System.out.print(strins.next() + " ");
}
It works perfectly but it just doesn't work when I use an Integer array..
Integer array[] [] = new Integer [3] [3];
for (int i=0; i<3; i++) {
for (int t=0; t<3; t++) {
array[i] [t] = 3;
}
}
MyIterator <Integer> it = new MyIterator <> (array);
while(it.hasNext()) {
System.out.println(it.next());
}
Here is my MyIterator class guys :
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyIterator <E> implements Iterator <E> {
private int i;
private E a[];
public MyIterator(E a[]) {
i=0;
this.a=a;
}
public boolean hasNext() {
return i<a.length;
}
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
} else {
return a[i++];
}
}
}
It says : Cannot infer type arguments for MyIterator<>
I would be very happy if people could help me since learning java is harder when the university is closed :(
If you guys see other things that could be improved, please tell me :)