Vector Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Row

    Vector Help

    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.lis tIterator();
    //while (Lists.hasNext( )) {
    String nextElement = (String)Lists.n ext();
    singleTextArea. setText(nextEle ment);
    }
    catch (ArrayIndexOutO fBoundsExceptio n f) {
    System.out.prin tln("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

    #2
    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@punka ss.com> wrote in message
    news:4d68ed39.0 404210254.5ebe6 9d3@posting.goo gle.com...[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.lis tIterator();
    > //while (Lists.hasNext( )) {
    > String nextElement = (String)Lists.n ext();
    > singleTextArea. setText(nextEle ment);
    > }
    > catch (ArrayIndexOutO fBoundsExceptio n f) {
    > System.out.prin tln("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]


    Comment

    • Row

      #3
      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.g oodsell@nopoo.b lueyonder.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]

      Comment

      • Roedy Green

        #4
        Re: Vector Help

        On 21 Apr 2004 03:54:51 -0700, soul_fly@punkas s.com (Row) wrote or
        quoted :
        [color=blue]
        > ListIterator Lists = stringArray.lis tIterator();[/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.

        Comment

        • SPG

          #5
          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(vectP ointer);
          vectPointer++;
          if( vectPointer >= myVect.Size()){
          val = null
          }
          return val;
          }

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

          Steve

          "Row" <soul_fly@punka ss.com> wrote in message
          news:4d68ed39.0 404211943.162e9 07d@posting.goo gle.com...[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.g oodsell@nopoo.b lueyonder.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]


          Comment

          • Row

            #6
            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.lis tIterator();
            //while (Lists.hasNext( )) {
            String nextElement = (String)Lists.n ext();
            singleTextArea. setText(nextEle ment);
            }
            catch (ArrayIndexOutO fBoundsExceptio n f) {
            System.out.prin tln("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.g oodsell@nopoo.b lueyonder.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]

            Comment

            • Christophe Vanfleteren

              #7
              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.lis tIterator();
              > //while (Lists.hasNext( )) {
              > String nextElement = (String)Lists.n ext();
              > singleTextArea. setText(nextEle ment);
              > }
              > catch (ArrayIndexOutO fBoundsExceptio n f) {
              > System.out.prin tln("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.lis tIterator()
              }
              if(listIterator .hasNext()) {
              singleTextArea. setText(listsIt erator.next().t oString());
              }
              }
              ....

              }

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

              --
              Kind regards,
              Christophe Vanfleteren

              Comment

              • Mark Haase

                #8
                Re: Vector Help!!

                In article <4d68ed39.04042 20553.310f27e@p osting.google.c om>,
                soul_fly@punkas s.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)e du

                Comment

                Working...