File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Overriding remove method of an iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding remove method of an iterator" Watch "Overriding remove method of an iterator" New topic
Author

Overriding remove method of an iterator

Rachit Pant
Greenhorn

Joined: Aug 01, 2011
Posts: 9
I am writing iterator method of a class.
The structure of class is as:

class A{
TypeX[] typex;
public Iterator<TypeX> iterator() {} // this is the method i wanna write
}

I tried this in my iterator methods code:

return Arrays.asList(typex).iterator();

This will return the Iterator<TypeX> as required , but i want my iterator to throw an UnsupportedOperationException, when i call its remove method.
So I need to override this method.
Can someone tell me how to do this??

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Rachit Pant wrote:I am writing iterator method of a class.
The structure of class is as:

class A{
TypeX[] typex;
public Iterator<TypeX> iterator() {} // this is the method i wanna write
}

I tried this in my iterator methods code:

return Arrays.asList(typex).iterator();

This will return the Iterator<TypeX> as required , but i want my iterator to throw an UnsupportedOperationException, when i call its remove method.
So I need to override this method.
Can someone tell me how to do this??




There is no black magic here... The easiest way to override a method, is to actually override the method. This means that you need access to the super class to inherit from. And since you don't, you can either (1) write a wrapper iterator or better yet (2) write an iterator (there is only three methods) that iterates your data structure.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rachit Pant
Greenhorn

Joined: Aug 01, 2011
Posts: 9
Can you write small piece of code to demonstrate this. I wanna learn both these ways . Or some pointers please.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

The Iterator returned by Arrays.asList(typex).iterator() already throws an UnsupportedOperationException when remove() is called. The List is not read-only, but its size cannot be changed. That means it's not possible to add or remove elements, only replace existing elements. In other words, you don't need to do anything in this case.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Note, though, that if this particular Iterator didn't already do what you wanted, you would have to find a different approach, such as writing your own Iterator implementation from scratch, because the implementations in the Core API Collections Framework are private and cannot be overridden.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Or you can wrap the existing Iterator into a new Iterator. The hasNext() and next() methods are built on top of the return values of the original Iterator. An example, from AbstractMap's keySet() method:
This iterator() implementation wraps the Iterator returned by entrySet().iterator(), no matter what that returns (that depends on the implementation from HashMap, TreeMap, etc). You could do something similar but let remove() throw an UnsupportedOperationException instead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding remove method of an iterator