• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Overriding remove method of an iterator  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??

 
author
Sheriff
Posts: 23589
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Rachit Pant
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write small piece of code to demonstrate this. I wanna learn both these ways . Or some pointers please.
 
Sheriff
Posts: 21461
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21461
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!