473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Vector Help

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
7 3986
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
4
by: wukexin | last post by:
I try some kind of compiler, my program compile succeeding, but run wrongly. Help me, see my program. If you have free time, please give me some suggestion about process command argument. Thank you...
9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
6
by: kittykat | last post by:
Hello, I am writing a program that will read each line of a file into a vector of vectors. This data will then be analysed. Here is what i have done: typedef vector<string> lines; ......
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
1
by: raylegendkiller | last post by:
NEED TO MAKE A PROGRAM which computes the current value of the vectors {x} based on the following forward iterations: this >>> {x}(n+1) = {x}(n), n = 0,1,2, ... ,8,9. In other...
10
by: JDT | last post by:
Hi, Can someone provide me an example that uses std::max_element() (probablly the predicate version) to return the max "absolute" integer in a vector? Your help is much appreciated. Tony ...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
0
by: doobybug | last post by:
Hi all, I really need your help! I am new to java and have some problems with my code. I have a program which inputs questionnaires and creates an object for each questionnaire. These objects are...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.