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