By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

Vector Help

P: n/a
Row
Hi,
Im relitivly new to java. I guess you could say im a newbie (even
though i studied it 3 years ago)

I have 2 simple questions. relating to java vectors

Question 1:
My application uses an Vector called stringArray. I am using a GUI to
access this Vector. I have 2 buttons. ("NEXT" and "PREVIOUS").
I would like to use these buttons to forwards or backwards through the
Vector Elements.
Now what i have below works. But once i push "Next" or "Previous" once
the applet just freezes and the record shown in the text area remains
the same. My code for this is below.

public void moveNext() {
try {
statLbl.setText("done");
ListIterator Lists = stringArray.listIterator();
//while (Lists.hasNext()) {
String nextElement = (String)Lists.next();
singleTextArea.setText(nextElement);
}
catch (ArrayIndexOutOfBoundsException f) {
System.out.println("No value at element");
statLbl.setText(" error ");
}
}

Question 2:
is there a way to see which element the array pointer is at?
For instance.
I need to show information on [Record No / Total Records]
I know to get the Total Records i could use:
vector.size();
But i how would i determine where i am in the array(pointer)?
I really need to finish this by friday but i have had trouble finding
what i need on the internet.
Thanks for your help in advance........i hope you can help another
newbie out!
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
SPG
You could use a LinkedList, that makes traversing back and forth simple.
Your code always gets a new iterator on each click, so you will always show
the first element in the list.

If you want to keep your code using the listIterator, then I suggest you use
a class level variable to store an instance of the iterator in.

Good luck,

Steve

"Row" <so******@punkass.com> wrote in message
news:4d**************************@posting.google.c om...
Hi,
Im relitivly new to java. I guess you could say im a newbie (even
though i studied it 3 years ago)

I have 2 simple questions. relating to java vectors

Question 1:
My application uses an Vector called stringArray. I am using a GUI to
access this Vector. I have 2 buttons. ("NEXT" and "PREVIOUS").
I would like to use these buttons to forwards or backwards through the
Vector Elements.
Now what i have below works. But once i push "Next" or "Previous" once
the applet just freezes and the record shown in the text area remains
the same. My code for this is below.

public void moveNext() {
try {
statLbl.setText("done");
ListIterator Lists = stringArray.listIterator();
//while (Lists.hasNext()) {
String nextElement = (String)Lists.next();
singleTextArea.setText(nextElement);
}
catch (ArrayIndexOutOfBoundsException f) {
System.out.println("No value at element");
statLbl.setText(" error ");
}
}

Question 2:
is there a way to see which element the array pointer is at?
For instance.
I need to show information on [Record No / Total Records]
I know to get the Total Records i could use:
vector.size();
But i how would i determine where i am in the array(pointer)?
I really need to finish this by friday but i have had trouble finding
what i need on the internet.
Thanks for your help in advance........i hope you can help another
newbie out!

Jul 17 '05 #2

P: n/a
Row
Thanks for the reply steve,

Unfortunatly i really have no idea about linked lists.
I would like the keep the Iterator, but again i have no idea how to
implement an iterator as a class level variable?
I have tried putint the new Iterator at the top of my main class.

but when i run the program it throws an exception relating to the
Iterator :(

Could you please describe how you would put the list Iterator in a
class method
So a new Iterator isn't created every time i press next / or previous.
like i said im new to this, so I would be so greatfull if you could
show me.!!!!!
thanks once again Steve and everyone else!

Rowan

"SPG" <st*******************@nopoo.blueyonder.co.uk> wrote in message news:<LF*****************@news-text.cableinet.net>...
You could use a LinkedList, that makes traversing back and forth simple.
Your code always gets a new iterator on each click, so you will always show
the first element in the list.

If you want to keep your code using the listIterator, then I suggest you use
a class level variable to store an instance of the iterator in.

Good luck,

Steve

Jul 17 '05 #3

P: n/a
On 21 Apr 2004 03:54:51 -0700, so******@punkass.com (Row) wrote or
quoted :
ListIterator Lists = stringArray.listIterator();

what you want is a simple private int to track where you are in the
Vector. You want to make sure it does not get too big or too small.
Iterators are used when you want to process all the elements in a
batch process. That is not what you are doing here.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #4

P: n/a
SPG
Rowan,

This is a simple example of how you should implement it..

public class MyClass{
private int vectPointer = 0;
private Vector myVect;

public MyClass(){
myVect = new Vector();
//Now do your population of the Vector...

//Set pointer to 0;
vectPointer = 0;
}

public String GetNext(){
String val = (String)myVect.elementAt(vectPointer);
vectPointer++;
if( vectPointer >= myVect.Size()){
val = null
}
return val;
}

public String GetPrevious(){
if( vectPointer == 0) return null;
vectPointer--;
return (String)myVect.elementAt(vectPointer);
}
}

Steve

"Row" <so******@punkass.com> wrote in message
news:4d**************************@posting.google.c om...
Thanks for the reply steve,

Unfortunatly i really have no idea about linked lists.
I would like the keep the Iterator, but again i have no idea how to
implement an iterator as a class level variable?
I have tried putint the new Iterator at the top of my main class.

but when i run the program it throws an exception relating to the
Iterator :(

Could you please describe how you would put the list Iterator in a
class method
So a new Iterator isn't created every time i press next / or previous.
like i said im new to this, so I would be so greatfull if you could
show me.!!!!!
thanks once again Steve and everyone else!

Rowan

"SPG" <st*******************@nopoo.blueyonder.co.uk> wrote in message

news:<LF*****************@news-text.cableinet.net>...
You could use a LinkedList, that makes traversing back and forth simple.
Your code always gets a new iterator on each click, so you will always show the first element in the list.

If you want to keep your code using the listIterator, then I suggest you use a class level variable to store an instance of the iterator in.

Good luck,

Steve

Jul 17 '05 #5

P: n/a
Row
Maybe i should elaborate a little more

My application uses an Vector called stringArray. I am using a GUI to
access this Vector. I have 2 buttons. ("NEXT" and "PREVIOUS").
I would like to use these buttons to forwards or backwards through the
Vector Elements.
Now what i have below works. But once i push "Next" or "Previous" once
the applet just freezes and the record shown in the text area remains
the same. My code for this is below.

public void moveNext() {
try {
statLbl.setText("done");
ListIterator Lists = stringArray.listIterator();
//while (Lists.hasNext()) {
String nextElement = (String)Lists.next();
singleTextArea.setText(nextElement);
}
catch (ArrayIndexOutOfBoundsException f) {
System.out.println("No value at element");
statLbl.setText(" error ");
}
}

My QUESTION:

How would i use a "Class level variable" to store an instance of the
iterator in.?? ( as steve mentioned in the quote below)
I understand this would stop me creating the ListIterator every time i
press next or previous ( thus solving my problem hopefully)???????????

Can someone help me out here???????

Rowan.


"SPG" <st*******************@nopoo.blueyonder.co.uk> wrote in message news:<LF*****************@news-text.cableinet.net>...
You could use a LinkedList, that makes traversing back and forth simple.
Your code always gets a new iterator on each click, so you will always show
the first element in the list.

If you want to keep your code using the listIterator, then I suggest you use
a class level variable to store an instance of the iterator in.

Good luck,

Steve

Jul 17 '05 #6

P: n/a
Row wrote:
Maybe i should elaborate a little more

My application uses an Vector called stringArray. I am using a GUI to
access this Vector. I have 2 buttons. ("NEXT" and "PREVIOUS").
I would like to use these buttons to forwards or backwards through the
Vector Elements.
Now what i have below works. But once i push "Next" or "Previous" once
the applet just freezes and the record shown in the text area remains
the same. My code for this is below.

public void moveNext() {
try {
statLbl.setText("done");
ListIterator Lists = stringArray.listIterator();
//while (Lists.hasNext()) {
String nextElement = (String)Lists.next();
singleTextArea.setText(nextElement);
}
catch (ArrayIndexOutOfBoundsException f) {
System.out.println("No value at element");
statLbl.setText(" error ");
}
}

My QUESTION:

How would i use a "Class level variable" to store an instance of the
iterator in.?? ( as steve mentioned in the quote below)
I understand this would stop me creating the ListIterator every time i
press next or previous ( thus solving my problem hopefully)???????????

Can someone help me out here???????

Rowan.


Please don't top-post.

Just declare a Listiterator in your class:

public class YourClass {
private ListIterator listIterator;

....
public void moveNext() {
if(listIterator == null) {
stringArray.listIterator()
}
if(listIterator.hasNext()) {
singleTextArea.setText(listsIterator.next().toStri ng());
}
}
....

}

Also, don't use entire rows of question marks. One is enough, any more than
that make you look stupid.

--
Kind regards,
Christophe Vanfleteren
Jul 17 '05 #7

P: n/a
In article <4d*************************@posting.google.com> ,
so******@punkass.com (Row) wrote:
My application uses an Vector called stringArray. I am using a GUI to
access this Vector. I have 2 buttons. ("NEXT" and "PREVIOUS").
I would like to use these buttons to forwards or backwards through the
Vector Elements.
Now what i have below works. But once i push "Next" or "Previous" once
the applet just freezes and the record shown in the text area remains
the same. My code for this is below.


are you trying to troll? you've already gotten your answer!

besides, in what sense does it "work" if as soon as you push a button it
freezes??

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.