Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > returning a pair of iterator.

Reply
Thread Tools

returning a pair of iterator.

 
 
toton
Guest
Posts: n/a
 
      10-04-2006
Hi,
I have a collection class (say ArrayList) which stores values. A
separate class Session holds the array list reference. I want the
session class to return a pair of iterator for the ArrayList for
iteration purpose (say from example 50 to 60 index only). And also want
the foreach loop to run over this range of iterators only. (Or may be a
single iterator, like all other Java iterators which has a hasNext ends
at the end index) . How to do it?
Also I want two type of iterators for the list (one will return a C++
like const_iterator where modification is not possible, I am not sure
If it at all can be done, as it needs something like returning final
reference, and one non const iterator.).
Any help is appreciated.

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      10-04-2006
toton wrote:

> I have a collection class (say ArrayList) which stores values. A
> separate class Session holds the array list reference. I want the
> session class to return a pair of iterator for the ArrayList for
> iteration purpose (say from example 50 to 60 index only). And also want
> the foreach loop to run over this range of iterators only. (Or may be a
> single iterator, like all other Java iterators which has a hasNext ends
> at the end index) . How to do it?
> Also I want two type of iterators for the list (one will return a C++
> like const_iterator where modification is not possible, I am not sure
> If it at all can be done, as it needs something like returning final
> reference, and one non const iterator.).
> Any help is appreciated.


I would spend a couple of weeks drinking hard until all memories of C++ have
been washed away (any form of alcohol should do the trick if taken in
sufficient quanties). Once you have achieved that, you should be able to
re-read the Collections documentation and tutorials without being confused by
memories of the Standard Template Library (which is build on fundamentally
different concepts from those in the Java collections library).

Then, when you've sobered up and the hangover has worn off, take a look at
java.util.Collections.unmodifiableList() and java.util.List.sublist().

-- chris



 
Reply With Quote
 
 
 
 
toton
Guest
Posts: n/a
 
      10-04-2006

Chris Uppal wrote:
> toton wrote:
>
> > I have a collection class (say ArrayList) which stores values. A
> > separate class Session holds the array list reference. I want the
> > session class to return a pair of iterator for the ArrayList for
> > iteration purpose (say from example 50 to 60 index only). And also want
> > the foreach loop to run over this range of iterators only. (Or may be a
> > single iterator, like all other Java iterators which has a hasNext ends
> > at the end index) . How to do it?
> > Also I want two type of iterators for the list (one will return a C++
> > like const_iterator where modification is not possible, I am not sure
> > If it at all can be done, as it needs something like returning final
> > reference, and one non const iterator.).
> > Any help is appreciated.

>
> I would spend a couple of weeks drinking hard until all memories of C++ have
> been washed away (any form of alcohol should do the trick if taken in
> sufficient quanties). Once you have achieved that, you should be able to
> re-read the Collections documentation and tutorials without being confused by
> memories of the Standard Template Library (which is build on fundamentally
> different concepts from those in the Java collections library).
>
> Then, when you've sobered up and the hangover has worn off, take a look at
> java.util.Collections.unmodifiableList() and java.util.List.sublist().

Thanks.
I get paid for C++ while programming with Java is my past time! Even
with lots of drink, I wont be able to forget it (Otherwise I wont be
able to get drink).
subList looks quite reasonable, as the Java List itself is nothing more
than iterator. Thus sublist looks like an iterator only. However I am
not convinced of unmodifiableList. As Java doesn't provide a way to
return final reference, how would it be possible! . I looked at the
Collections code curiously and found all of the methods throws some
UnsupportedOperation exception. That says that the list can not be
modified. However I want the list element should not be able to
modified Just like const_iterator in C++. As the List returns a get
reference to object, the object always can be modified.
Just from the following code, it is evident.
import java.util.*;
class MyClass{
public int x;
public MyClass(int x){
this.x = x;

}
public String toString(){
return Integer.toString(x);
}
};
class Test{
public static void main(String[] args){
ArrayList list = new ArrayList(10);
list.add(new MyClass(10));
list.add(new MyClass(11));
list.add(new MyClass(12));
list.add(new MyClass(13));
list.add(new MyClass(14));
list.add(new MyClass(15));
list.add(new MyClass(16));
list.add(new MyClass(17));
System.out.println(list);
List range = list.subList(2,4);
System.out.println(range);
List cList = Collections.unmodifiableList(range);
System.out.println(cList);
MyClass c = (MyClass)cList.get(0);
c.x = 14;
System.out.println(((MyClass)cList.get(0)).x);
System.out.println(cList);


}
};
Ignore, 1.5 warnings. I compiled it with non-generic option.
I want the list element not to get modified (with or without iterator).
Something like C++ const_iterator. Can it be done directly or
indirectly? Ofcourse, other non const reference to the element may be
able to modify it. However I want unmodifiableList , or whatever it may
be wont be able to modify it.

Thanks for help & quick answer.
> -- chris


 
Reply With Quote
 
Tim Ward
Guest
Posts: n/a
 
      10-04-2006
"toton" <> wrote in message
news: oups.com...
>
> Just like const_iterator in C++.


As Chris Uppal says, you're on a loser if you're wanting stl-like
functionality from Java.

--
Tim Ward
Brett Ward Limited - www.brettward.co.uk


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      10-04-2006

"toton" <> wrote in message
news: oups.com...
>
> I want the list element not to get modified (with or without iterator).
> Something like C++ const_iterator. Can it be done directly or
> indirectly?


Have your elements implement some sort of interface which provides all
the "getter" and non mutating functionality, and have the iterator wrap the
elements in an immutable wrapper (or copy):

interface Foo {
public int getX();
}

class MutableFoo implements Foo {
private int myX;
public int getX() {
return myX;
}
public void setX(int newValue) {
myX = newValue;
}
}

class ImmutableFoo implements Foo {
private final int myX;
public ImmutableFoo(int value) {
myX = value;
}
public ImmutableFoo(Foo value) {
myX = value.getX();
}
public int getX() {
return myX;
}
}

class ConstIterator<Foo> implements Iterator<Foo> {
private final Iterator<Foo> delegateIterator;

public ConstIterator<Foo>(Iterator<Foo> delegate) {
delegateIterator = delegate;
}
public boolean hasNext() {
delegateIterator.hasNext();
}
public Foo next() {
return new ImmutableFoo(delegateIterator.next());
}
public void remove() {
/*I don't know what C++'s const_iterator does here, but you should be
able to figure it out from here on.*/
}
}

Note that a really determine programmer can still modify the elements
using sneaky stuff like reflection or JNI.

- Oliver


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      10-04-2006
toton wrote:
> I get paid for C++


My condolences

> That says that the list can not be
> modified. However I want the list element should not be able to
> modified Just like const_iterator in C++.


You won't get a const_itertator. Java is not C++.

a) You will hopefully soon find out that it is typically a waste of time
trying to protect objects from getting modified. If you have designed
your objects in the right way (they manage to keep themselves
consistent), it should be perfectly ok to modify an object. Totally
independent of who does it. If you don't trust someone with your
objects, don't give them the objects at all. If you trust them, let them
do whatever they want.


> As the List returns a get
> reference to object, the object always can be modified.


b) No, not always. Only objects which allow others to modify them can be
modified. If you want objects which can't be modified, add only objects
to the list which don't provide methods for modification (e.g. immutable
objects). What? Your objects provide methods for modification? Well,
then wrap them in some immutable wrapper object before you add them to
the list. Typically, however, that is a waste of time and resources.


public class ValuableObject {
public int getData() { ...}
public void setData(int data) { ... }
}

public class ReallyValuableProtectedObject {
private ValuableObject vo;

public ReallyValuableProtectedObject(ValuableObject vo) {
this.vo = vo;
}
public int getData() { return vo.getData(); }

// do not implement setData() or implement
// it as a no-op
}

You can decorate all this with the usage of some common interface, e.g.:

interface ReadableValuableObject {
public int getData();
}

public class ValuableObject implements ReadableValuableObject { ... }
public class ReallyValuableProtectedObject implements
ReadableValuableObject { ... }


And when you have done that a few hundred times you might recognize that
you are wasting your time.

/Thomas
--
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hie...lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
bugbear
Guest
Posts: n/a
 
      10-04-2006
toton wrote:
> Hi,
> I have a collection class (say ArrayList) which stores values. A
> separate class Session holds the array list reference. I want the
> session class to return a pair of iterator for the ArrayList for
> iteration purpose (say from example 50 to 60 index only). And also want
> the foreach loop to run over this range of iterators only. (Or may be a
> single iterator, like all other Java iterators which has a hasNext ends
> at the end index) . How to do it?
> Also I want two type of iterators for the list (one will return a C++
> like const_iterator where modification is not possible, I am not sure
> If it at all can be done, as it needs something like returning final
> reference, and one non const iterator.).
> Any help is appreciated.
>


Fun stuff here:

http://jakarta.apache.org/commons/co...e-summary.html

BugBear
 
Reply With Quote
 
Bill Medland
Guest
Posts: n/a
 
      10-04-2006
toton wrote:

>
> Chris Uppal wrote:
>> toton wrote:
>>
>> > I have a collection class (say ArrayList) which stores values. A
>> > separate class Session holds the array list reference. I want the
>> > session class to return a pair of iterator for the ArrayList for
>> > iteration purpose (say from example 50 to 60 index only). And also want
>> > the foreach loop to run over this range of iterators only. (Or may be a
>> > single iterator, like all other Java iterators which has a hasNext ends
>> > at the end index) . How to do it?
>> > Also I want two type of iterators for the list (one will return a C++
>> > like const_iterator where modification is not possible, I am not sure
>> > If it at all can be done, as it needs something like returning final
>> > reference, and one non const iterator.).
>> > Any help is appreciated.

>>
>> I would spend a couple of weeks drinking hard until all memories of C++
>> have been washed away (any form of alcohol should do the trick if taken
>> in
>> sufficient quanties). Once you have achieved that, you should be able to
>> re-read the Collections documentation and tutorials without being
>> confused by memories of the Standard Template Library (which is build on
>> fundamentally different concepts from those in the Java collections
>> library).
>>
>> Then, when you've sobered up and the hangover has worn off, take a look
>> at java.util.Collections.unmodifiableList() and java.util.List.sublist().

> Thanks.
> I get paid for C++ while programming with Java is my past time! Even
> with lots of drink, I wont be able to forget it (Otherwise I wont be
> able to get drink).
> subList looks quite reasonable, as the Java List itself is nothing more
> than iterator. Thus sublist looks like an iterator only. However I am
> not convinced of unmodifiableList. As Java doesn't provide a way to
> return final reference, how would it be possible! . I looked at the
> Collections code curiously and found all of the methods throws some
> UnsupportedOperation exception. That says that the list can not be
> modified. However I want the list element should not be able to
> modified Just like const_iterator in C++. As the List returns a get
> reference to object, the object always can be modified.
> Just from the following code, it is evident.
> import java.util.*;
> class MyClass{
> public int x;
> public MyClass(int x){
> this.x = x;
>
> }
> public String toString(){
> return Integer.toString(x);
> }
> };
> class Test{
> public static void main(String[] args){
> ArrayList list = new ArrayList(10);
> list.add(new MyClass(10));
> list.add(new MyClass(11));
> list.add(new MyClass(12));
> list.add(new MyClass(13));
> list.add(new MyClass(14));
> list.add(new MyClass(15));
> list.add(new MyClass(16));
> list.add(new MyClass(17));
> System.out.println(list);
> List range = list.subList(2,4);
> System.out.println(range);
> List cList = Collections.unmodifiableList(range);
> System.out.println(cList);
> MyClass c = (MyClass)cList.get(0);
> c.x = 14;
> System.out.println(((MyClass)cList.get(0)).x);
> System.out.println(cList);
>
>
> }
> };
> Ignore, 1.5 warnings. I compiled it with non-generic option.
> I want the list element not to get modified (with or without iterator).
> Something like C++ const_iterator. Can it be done directly or
> indirectly? Ofcourse, other non const reference to the element may be
> able to modify it. However I want unmodifiableList , or whatever it may
> be wont be able to modify it.
>
> Thanks for help & quick answer.
>> -- chris


Since no-one has said it in words of one syllable and I fell over it
yesterday, note that 'final' applied to a reference does NOT mean that the
object is not modifiable; it means the reference is not modifiable (it
can't refer to something else but what it does refer to can mutate)
--
Bill Medland
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      10-04-2006
toton wrote:

> However I am
> not convinced of unmodifiableList. As Java doesn't provide a way to
> return final reference, how would it be possible!


Yes, that's right. There simply is no equivalent of C++ pointer-to-const in
Java.

-- chris


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      10-04-2006

"Chris Uppal" <> wrote in message
news:4523de7f$0$631$...
> toton wrote:
>
>> However I am
>> not convinced of unmodifiableList. As Java doesn't provide a way to
>> return final reference, how would it be possible!

>
> Yes, that's right. There simply is no equivalent of C++ pointer-to-const
> in
> Java.


To possibly clarify the OP's confusion, in an unmodifiableList (as
returned by Collections), it's the list which is unmodifiable (i.e. you
can't add or remove elements to it), not the elements contained within the
list.

- Oliver


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Range on a pair of Belkin 54G routers in wireless bridge mode? Bill Evans Wireless Networking 5 02-03-2005 03:14 AM
IAS AV - Pair ? jt Cisco 0 11-04-2003 10:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57