5 Replies - 7868 Views - Last Post: 19 November 2007 - 06:09 PM Rate Topic: -----

#1 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

is not abstract and does not override abstract method (next)

Posted 19 November 2007 - 05:27 PM

error : is not abstract and does not override abstract method (next) in java.util.iterator

how do i set up my next() to overrride the iterator?
cause it looks like it does to me....

import java.util.*;
public class IteratorTest implements Iterator 
{
	private int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
	private int nextNum;
	
	public IteratorTest(){
	   nextNum = 0;
	}
	
	public <E> int next(){
	nextNum += 1;
	return nums[nextNum-1];
	}
	
	public void remove(){
	System.out.println("Remove is not allowed");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: is not abstract and does not override abstract method (next)

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: is not abstract and does not override abstract method (next)

Posted 19 November 2007 - 05:42 PM

This bit doesn't make sense public <E> int next() . It's either an int or an <E>.

Other than that, depends on the versions of java you're using. I don't see a boolean hasNext(), which is also required for the basic, non generic, Iterator interface.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: is not abstract and does not override abstract method (next)

Posted 19 November 2007 - 05:46 PM

it should be an <e> so says the documentation,
so i take out the int and it gives me a return type required error.

its still that same error though,
i threw in a hasNext but it didn't help and it specifically says I'm not overriding next.

Its java 6 update 3 , but not seeing my issue yet.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4356
  • View blog
  • Posts: 12,175
  • Joined: 18-April 07

Re: is not abstract and does not override abstract method (next)

Posted 19 November 2007 - 05:50 PM

This is your code on steroids. I made your iterator handle objects, as most do, and you have to cast back to your desired value. Here we loop through the numbers of your array and implement all methods of the interface. Your public <E> int next() didn't exactly match the iterator interface definition, so it was throwing your error.

import java.util.*;
public class IteratorTest implements Iterator
{
	private int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	private int nextNum;
	
	public IteratorTest(){
	   nextNum = 0;
	}
	
	// Iterators return objects to match Iterator interface definition
	public Object next(){
		return nums[nextNum++];
	}

	// Must have hasNext also implemented as part of interface definition
	public boolean hasNext() { return nextNum < 10; }
	
	public void remove(){
		System.out.println("Remove is not allowed");
	}

 
	public static void main(String args[]) {

		// Create an iterator to loop through, cast each from object to Integer type
		// Not int because int is primitive and object can't be cast directly to int.

		for (Iterator i= new IteratorTest(); i.hasNext(); ) {
				Integer mynumber = (Integer)i.next();
				System.out.println("Next Number is: " + mynumber);
		}
	}
}



So play with that and I am sure you can get the gist of it really quick. Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4356
  • View blog
  • Posts: 12,175
  • Joined: 18-April 07

Re: is not abstract and does not override abstract method (next)

Posted 19 November 2007 - 06:05 PM

Now if you are going the full generics route, you are going to write and change quite a bit more. Try this out...

import java.util.*;
public class IteratorTest<E> implements Iterator<E>
{
	// Create a generic array of Integer types
	private E[] nums = (E[]) new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	private int nextNum;
	
	public IteratorTest(){
	   nextNum = 0;
	}
	
	// Iterators return objects to match Iterator interface definition, in this case E
	public E next(){
		return nums[nextNum++];
	}

	// Must have hasNext also implemented as part of interface definition
	public boolean hasNext() { return nextNum < 10; }
	
	public void remove(){
		System.out.println("Remove is not allowed");
	}

 
	public static void main(String args[]) {

		// Create an iterator to loop through, cast each from object to Integer type
		// Not int because int is primitive and object can't be cast directly to int.
		// Uses our generics to make them Integer types

		for (Iterator<Integer> i= new IteratorTest<Integer>(); i.hasNext(); ) {
				Integer mynumber = (Integer)i.next();
				System.out.println("Next Number is: " + mynumber);
	
		}
	}  
}



Hope that is making sense to you as well. :)
Was This Post Helpful? 0
  • +
  • -

#6 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: is not abstract and does not override abstract method (next)

Posted 19 November 2007 - 06:09 PM

Major props my man , that was a huge help. Now I actually understand what I'm doing. I think I stared at the documentation and my book for too long on a simple problem and couldn't figure it out , your answer cleared it up, and now I can do part two . hooray!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1