wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes iterating through vector - ListIterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "iterating through vector - ListIterator" Watch "iterating through vector - ListIterator" New topic
Author

iterating through vector - ListIterator

chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hi
How do you iterate through a Vector? Tried using ListIterator..
import java.util.*;
...
....
Vector vSub = getSub(...);
ListIterator ltr = vSub.listIterator();

but this throws an exception..msg...'java.util.AbstractList'.
Using ListIterator so that i can move forward and backwards through the list. Any idea on how to move back and forth in a vector? Even if an element is removed in the vector it must work.
pl help
cds
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Don't use Vector, use ArrayList.
You can then use Iterator.


42
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
The inheritance heirarchy of Vector follows:

java.lang.Object
extended byjava.util.AbstractCollection
extended byjava.util.AbstractList
extended byjava.util.Vector

AbstractList has a method called iterator (at least in jdk 1.4), and another one called listIterator. Are you saying they don't work? As far as iterating through the Vector and allowing iteration to continue if someone removes an Object. I don't think Iterators work this way. You could syncronize and make it so when iterating noone can change the vector. Also, I would use ArrayList over Vector.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Maybach Smith
Greenhorn

Joined: Dec 22, 2004
Posts: 15
Still,

Vector vec = new Vector();
ListIterator iter = vec.listIterator(); is perfectly valid as Vector extends from AbstractList and no
message should be thrown as cb gthri has pointed out

Am i correct ? Please correct me if i am wrong.This is just for my understanding.

Using ArrayList as pointed by all would be better

[ May 18, 2005: Message edited by: Maybach Smith ]
[ May 18, 2005: Message edited by: Maybach Smith ]

Maybach
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello
Thanks for all the replies.
I am using JDK 1.3.1_06 for compiling.
As Maybach mentioned, ListIterator iter = vec.listIterator(); is perfectly valid i think.
As i mentioned in an earlier question in the forum,it is an applet-servlet comm.The servlet sending back a vector and the applet displaying it.
Will i abe able to move forward/backward in an ArrayList?Also, if an element is removed,i must still be able to move back and forth.ListIterator fits well there.

The exception 'java.util.AbstractList' persists .
rgds
csb
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
And what is the advantage of using ArrayList over a Vector?
csb
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Invoking a Vector's listIterator() should not cause an exception. Please post the whole of the exception text which you are receiving.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello Roger,

This is the code..

import java.util.*;
...
....
ListIterator ltr;
...
Vector vSub = getSub(...);
...
try{
ltr = vSub.listIterator();
}catch(Exception e){
e.getMessage();
}

i put it in a simple try-catch.i got just that single line errors msg.
Do i need no make any changes in the try-catch?
rgds
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
This looks strange, try e.printStackTrace() to see if any more information is provided.
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello Roger
I missed one point.pl note that these errors are occuring at the client side..ie.when running the applet.Perhaps this may not be the right place for this question,but anyway...
Tried using e.printStackTrace.But was unable to compile..the msg being " 'void' type not allowed here" of e.getMessage().Perhaps you can't use that in applet?

I also tried using ArrayList in the applet.But as soon as it is created the exception
'exception:java.lang.ClassNotFoundException:java.util.ArrayList' is thrown and the applet stops.Why is this?

rgds
csb
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Post yout applet code.
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello Roger
Here's part of the code....
import java.util.*;
...
Vector vSub;
ListIterator ltr;

public void init(){
vSub = new Vector();
}
...
public Vector getSub(.....){//fn that returns a vector
....
return (Vector)objC.recObj(con);
}
...
...

if(e.getSelectableItem()==cS){
vSub = null;
vSub = getSub(...);//calls the above fn

try{
ltr = vSub.listIterator();
}catch(Exception e){
e.getMessage();
}
...
...
}
and then you get the exception msg - 'java.util.AbstractList'
rgds
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
1. The biggest difference between Vectors and ArrayList is that while Vectors are synchronized (thread-safe), ArrayList is not. and so an ArrayList is less 'expensive'.

2. I am using jdk1.4 and using ListIterator li = vector.listIterator() works fine on my machine. does that have anything to do with jdk1.3? Werent the implementation of the legacy classes changed in 1.4?

3.


replace e.getMessage() with e.printStackTrace(), not something like System.out.println(e.printStackTrace())
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Moving to Java in General (Beginner)
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello Neeraj,
Tried e.printStackTrace() in the catch.But then i am not able to compile..

'void' type not allowed here
e.printStackTrace();
is the message given by the compiler.
rgds
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
cb, I would really recommend that you do not edit the code which you post, as you can easily remove something important. Please repost the entire code and be sure to paste the complete exception message which you receive.
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello Roger
Thanks again Roger for your willingness to help.So also to the other members.
The applet code is a bit large. If u can give a mail id ,i can send you the file.You can post what went wrong in the code, in this forum,so that it may help others also.
If not i will post it here.
rgds
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It would probably be helpful if you post the EXACT error message first. That way we can help you track down which part of the code causes this error. Please copy and past the full error message that you get.

Layne


Java API Documentation
The Java Tutorial
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If your applet code really is big, maybe we can help you to trim it to a more sensible size.
chelakkad ben
Ranch Hand

Joined: Feb 09, 2005
Posts: 62
Hello Layne
The error message i am getting (e.getMessage()) is just a single line....
'java.util.AbstractList' . Nothing else.

I shall post the code tomorrow
rgds
 
GeeCON Prague 2014
 
subject: iterating through vector - ListIterator