• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

iterating through vector - ListIterator

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use Vector, use ArrayList.
You can then use Iterator.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
chelakkad ben
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what is the advantage of using ArrayList over a Vector?
csb
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Invoking a Vector's listIterator() should not cause an exception. Please post the whole of the exception text which you are receiving.
 
chelakkad ben
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks strange, try e.printStackTrace() to see if any more information is provided.
 
chelakkad ben
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post yout applet code.
 
chelakkad ben
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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())
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java in General (Beginner)
 
chelakkad ben
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your applet code really is big, maybe we can help you to trim it to a more sensible size.
 
chelakkad ben
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic