• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Overriding remove method of an iterator

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic