1 2 Previous Next 16 Replies Latest reply: Nov 10, 2009 10:08 AM by YoungWinston RSS

    Array to vector conversion

    843789
      Hey i'm trying to convert my array code to a vector, here is my array code :
      //  Chp 3.1.3
      //  Patrick Jurkiewicz
      import javax.swing.*;
      import java.io.*;
      
      public class HW415
      {
           final int MAXQUEUE = 100;
           Object[] items;
           int front, rear;
      
                          public HW415() {// Public class needs this name
                          items = new Object[MAXQUEUE];
                          front = items.length - 1;
                          rear = items.length - 1;
                         }
      
                          public boolean empty() {
                          if (front == rear)
                               return true;
                               else
                                    return false;
                               }
      
                     public Object remove(){
                          if (empty()) {
                               System.out.println("Queue Underflow.");
                               System.exit(1);
                          }
      
                          if (front == items.length - 1)
                               front = 0;
                          else
                               front++;
      
                               return items[front];
                          }
      
                     public void insert (Object x) {
                          if (rear == items.length - 1)
                          rear = 0;
                          else
                          rear++;
                          items[rear] = x;
      
                          }
      
      }
      And my Vector conversion:
      //  Chp 3.1.3
      //  Patrick Jurkiewicz
      import javax.swing.*;
      import java.io.*;
      import java.util.*;
      
      public class VectorObjectQueue
      {
           final int MAXQUEUE = 100;
           Vector items = new Vector();
           int front, rear;
      
                          public VectorObjectQueue() {// Public class needs this name
                          //Vector items = new Vector();
                          front = items.size() - 1;
                          rear = items.size() - 1;
                         }
      
                          public boolean empty() {
                          if (front == rear)
                               return true;
                               else
                                    return false;
                               }
      
                     public Object remove(){
                          if (empty()) {
                               System.out.println("Queue Underflow.");
                               System.exit(1);
                          }
      
                          if (front == items.size()- 1)
                               front = 0;
                          else
                               front++;
      
                               return items.lastElement;
                          }
      
                     public void insert (Object x) {
                          if (rear == items.size() - 1)
                          rear = 0;
                          else
                          rear++;
                          items.firstElement() = x;
      
                          }
      }
      I'm pretty sure i don't need the maxqueue since its a vector. When i compile i get this error
      .java:37: cannot find symbol
      symbol : variable lastElement
      location: class java.util.Vector
                               return items.lastElement;
                               ^
      .java:45: unexpected type
      required: variable
      found : value
                          items.firstElement() = x;
                          ^
      2 errors

      Any help is greatly appreciated
        • 1. Re: Array to vector conversion
          843789
          Vector.lastElement is a method. As such, you have to call it with an empty pair of braces "()".

          Edit: Now that the forums are alive again, I'll write the rest of my post:

          Also, Vector is a legacy class. Unless you are using it in a multi-threaded manner, there's no reason to use it over the more generally used ArrayList. I find the only reason people use it is because really old lectures/tutorials speak about Vector.

          Edited by: endasil on 9-Nov-2009 1:28 PM
          • 2. Re: Array to vector conversion
            843789
            It is for a homework assignment, it is to write a queue as a array, and convert it to a vector. So i have the array complete, but I'm having difficulties converting it to a vector.

            Now there is one error
            java:45: unexpected type
            required: variable
            found : value
                                items.firstElement() = x;
                                ^
            1 error

            Tool completed with exit code 1

            Edited by: maggiemutt on Nov 9, 2009 10:36 AM
            • 3. Re: Array to vector conversion
              843789
              items.firstElement() = x;
              Here you're trying to assign a method a value! This is of course impossible. What you're probably looking for are:
              items.set(0, x); //sets x as the first element
              items.set(items.size()-1, x); //sets x as the last element
              • 4. Re: Array to vector conversion
                843789
                Okay so this is the revised insert/remove code
                               public Object remove(){
                                    if (empty()) {
                                         System.out.println("Queue Underflow.");
                                         System.exit(1);
                                    }
                
                                    if (front == items.size()- 1)
                                         front = 0;
                                    else
                                         front++;
                
                                         return items.lastElement();
                                    }
                
                               public void insert (Object x) {
                                    if (rear == items.size() - 1)
                                    rear = 0;
                                    else
                                    rear++;
                                    items.set(0,x);
                
                                    }
                However now when i compile i get Note: \VectorObjectQueue.java uses unchecked or unsafe operations.
                Note: Recompile with -Xlint:unchecked for details.

                Tool completed successfully

                When i run it it says "Exception in thread "main" java.lang.NoSuchMethodError: main"

                What is that error saying?
                • 5. Re: Array to vector conversion
                  843789
                  maggiemutt wrote:
                  However now when i compile i get Note: \VectorObjectQueue.java uses unchecked or unsafe operations.
                  Note: Recompile with -Xlint:unchecked for details.
                  It's because you're not using Generics with Vector, which should be parameterized. I'd suggest holding off learning Generics at this point, but definitely study them in the future.
                  When i run it it says "Exception in thread "main" java.lang.NoSuchMethodError: main"

                  What is that error saying?
                  It's saying what it's saying: Java can't find a method named "main" in the class that you're trying to run. Do you have a static method named "main" that takes an array of Strings? Is it in the class that you're passing as an argument to "java"?
                  • 6. Re: Array to vector conversion
                    843789
                    Yes, here is the other issue i run into, my code:
                    class VectorObjectQueuetest{
                         public static void main (String[] args){
                         VectorObjectQueue q = new VectorObjectQueue();
                          int a =1, b=2, c=3, d=4, e=5;
                         q.insert(a);
                         q.insert(b);
                         System.out.println(q);
                              }
                         }
                    compile's fine, but when i run it i get:
                    Exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
                    at java.util.Vector.set(vector.java:712)
                    at VectorObjectQueue.insert(VectorObjectQueue.java:45)
                    at VectorObjectQueuetest.main(VectorObjectQueuetest.java:5)

                    Is this saying that the vector was never initialized?
                    • 7. Re: Array to vector conversion
                      843789
                      maggiemutt wrote:
                      compile's fine, but when i run it i get:
                      Exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
                      Yeah, that'll happen. It's because the whole point of a Vector is that it's dynamic in size. It doesn't have an element 0 until you add an element, so you can't just go setting wherever you want.

                      If you want a VectorArray of size 100, you would need to add 100 nulls to the vector to prime it.

                      But the whole idea of trying to make a List (Vector) act as an Array makes me want to vomit. To the point where I don't really care to continue to help. It's a ridiculous assignment, if that's what it is. If it's not an assignment, abandon this immediately. If your goal is to make a Queue, why not just use add(0, x) instead of masquerading like it's an array, when really it's not?!?!

                      Edited by: endasil on 9-Nov-2009 4:45 PM

                      Edit I just reread your post. Realize now that it used to be backed by an Array. Fair enough, just eliminate any concept in your brain that a Vector is like an array, use add(int, Object) to enqueue (insert), and remove(list.size()-1) to dequeue (remove).

                      Edited by: endasil on 9-Nov-2009 4:46 PM
                      • 8. Re: Array to vector conversion
                        843789
                        hahaha, i like your analogy, and know how you feel. yes this is an assignment. It doesn't need to be 100 items, i just need a vector queue that i can add, subtract etc with.
                        • 9. Re: Array to vector conversion
                          DrClap
                          endasil wrote:
                          If you want a VectorArray of size 100, you would need to add 100 nulls to the vector to prime it.

                          But the whole idea of trying to make a List (Vector) act as an Array makes me want to vomit. To the point where I don't really care to continue to help. It's a ridiculous assignment, if that's what it is.
                          That was sort of my opinion too. So just the other day I was cleaning up some not-so-old code, and I noticed it was using Vector. Okay, thinks I, let's just change that to ArrayList and carry on. But no... now there were errors in the code. Scroll down to where the error was, and... what's this "setSize()" method?

                          Find the API documentation, read it and weep.
                          • 10. Re: Array to vector conversion
                            843789
                            Edit I just reread your post. Realize now that it used to be backed by an Array. Fair enough, just eliminate any concept in your brain that a Vector is like an array, use add(int, Object) to enqueue (insert), and remove(list.size()-1) to dequeue (remove).


                            i'm a little confused by what you mean by that?
                            • 11. Re: Array to vector conversion
                              843789
                              maggiemutt wrote:
                              Edit I just reread your post. Realize now that it used to be backed by an Array. Fair enough, just eliminate any concept in your brain that a Vector is like an array, use add(int, Object) to enqueue (insert), and remove(list.size()-1) to dequeue (remove).


                              i'm a little confused by what you mean by that?
                              I mean, in your insert method, do vector.add(0, x) to add the element to the beginning. When you want to remove off the end, call vector.remove(vector.size()-1) to remove (and return) the last element.

                              What are you confused about?
                              • 12. Re: Array to vector conversion
                                843789
                                That reply was a lil more clear to me
                                • 13. Re: Array to vector conversion
                                  843789
                                  When i try to insert a number, i believe it is working, but when i try to display it, i am trying to display the object i believe and all i see is the memory referance. What is it that i should be displaying instead to see the actual value in place?
                                  class VectorObjectQueuetest{
                                       public static void main (String[] args){
                                       VectorObjectQueue q = new VectorObjectQueue();
                                        int a =1, b=2, c=3, d=4, e=5;
                                       q.insert(a);
                                       q.insert(b);
                                       System.out.println(q);
                                            }
                                       }
                                  • 14. Re: Array to vector conversion
                                    843789
                                    throws TooManyHandholdingRequestsException
                                    1 2 Previous Next