Connecting Tech Pros Worldwide Forums | Help | Site Map

Vector Help

Row
Guest
 
Posts: n/a
#1: Jul 18 '05
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!

SPG
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Vector Help


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" <soul_fly@punkass.com> wrote in message
news:4d68ed39.0404210254.5ebe69d3@posting.google.c om...[color=blue]
> 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![/color]


Row
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Vector Help


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" <steve.nospoo.goodsell@nopoo.blueyonder.co.uk> wrote in message news:<LFvhc.27$T87.493699@news-text.cableinet.net>...[color=blue]
> 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
>[/color]
Roedy Green
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Vector Help


On 21 Apr 2004 03:54:51 -0700, soul_fly@punkass.com (Row) wrote or
quoted :
[color=blue]
> ListIterator Lists = stringArray.listIterator();[/color]
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.
SPG
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Vector Help


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" <soul_fly@punkass.com> wrote in message
news:4d68ed39.0404211943.162e907d@posting.google.c om...[color=blue]
> 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" <steve.nospoo.goodsell@nopoo.blueyonder.co.uk> wrote in message[/color]
news:<LFvhc.27$T87.493699@news-text.cableinet.net>...[color=blue][color=green]
> > 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[/color][/color]
show[color=blue][color=green]
> > the first element in the list.
> >
> > If you want to keep your code using the listIterator, then I suggest you[/color][/color]
use[color=blue][color=green]
> > a class level variable to store an instance of the iterator in.
> >
> > Good luck,
> >
> > Steve
> >[/color][/color]


Row
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Vector Help


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.

[color=blue]
>
> "SPG" <steve.nospoo.goodsell@nopoo.blueyonder.co.uk> wrote in message news:<LFvhc.27$T87.493699@news-text.cableinet.net>...[color=green]
> > 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
> >[/color][/color]
Christophe Vanfleteren
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Vector Help


Row wrote:
[color=blue]
> 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.[/color]

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
Mark Haase
Guest
 
Posts: n/a
#8: Jul 18 '05

re: Vector Help


In article <4d68ed39.0404220553.310f27e@posting.google.com> ,
soul_fly@punkass.com (Row) wrote:
[color=blue]
> 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.[/color]

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
Closed Thread