Vector in java 5

How to use the new vector<E> class. We need to add all java types in vector.

you dont have to put that there but you can makes life easier as then you dont need to cast values that are pased back from things like vector.elementAt(0);
David

Similar Messages

  • Using Vector in Java script

    I got this vector class from some website for implementation of vector in java script
    function Vector(inc) {
         if (inc == 0) {
              inc = 100;
         /* Properties */
         this.data = new Array(inc);
         this.increment = inc;
         this.size = 0;
         /* Methods */
         this.getCapacity = getCapacity;
         this.getSize = getSize;
         this.isEmpty = isEmpty;
         this.getLastElement = getLastElement;
         this.getFirstElement = getFirstElement;
         this.getElementAt = getElementAt;
         this.addElement = addElement;
         this.insertElementAt = insertElementAt;
         this.removeElementAt = removeElementAt;
         this.removeAllElements = removeAllElements;
         this.indexOf = indexOf;
         this.contains = contains
         this.resize = resize;
         this.toString = toString;
         this.sort = sort;
         this.trimToSize = trimToSize;
         this.clone = clone;
         this.overwriteElementAt;
    // getCapacity() -- returns the number of elements the vector can hold
    function getCapacity() {
         return this.data.length;
    // getSize() -- returns the current size of the vector
    function getSize() {
         return this.size;
    // isEmpty() -- checks to see if the Vector has any elements
    function isEmpty() {
         return this.getSize() == 0;
    // getLastElement() -- returns the last element
    function getLastElement() {
         if (this.data[this.getSize() - 1] != null) {
              return this.data[this.getSize() - 1];
    // getFirstElement() -- returns the first element
    function getFirstElement() {
         if (this.data[0] != null) {
              return this.data[0];
    // getElementAt() -- returns an element at a specified index
    function getElementAt(i) {
         try {
              return this.data;
         catch (e) {
              return "Exception " + e + " occured when accessing " + i;     
    // addElement() -- adds a element at the end of the Vector
    function addElement(obj) {
         if(this.getSize() == this.data.length) {
              this.resize();
         this.data[this.size++] = obj;
    // insertElementAt() -- inserts an element at a given position
    function insertElementAt(obj, index) {
         try {
              if (this.size == this.capacity) {
                   this.resize();
              for (var i=this.getSize(); i > index; i--) {
                   this.data[i] = this.data[i-1];
              this.data[index] = obj;
              this.size++;
         catch (e) {
              return "Invalid index " + i;
    // removeElementAt() -- removes an element at a specific index
    function removeElementAt(index) {
         try {
              var element = this.data[index];
              for(var i=index; i<(this.getSize()-1); i++) {
                   this.data[i] = this.data[i+1];
              this.data[getSize()-1] = null;
              this.size--;
              return element;
         catch(e) {
              return "Invalid index " + index;
    // removeAllElements() -- removes all elements in the Vector
    function removeAllElements() {
         this.size = 0;
         for (var i=0; i<this.data.length; i++) {
              this.data[i] = null;
    // indexOf() -- returns the index of a searched element
    function indexOf(obj) {
         for (var i=0; i<this.getSize(); i++) {
              if (this.data[i] == obj) {
                   return i;
         return -1;
    // contains() -- returns true if the element is in the Vector, otherwise false
    function contains(obj) {
         for (var i=0; i<this.getSize(); i++) {
              if (this.data[i] == obj) {
                   return true;
         return false;
    // resize() -- increases the size of the Vector
    function resize() {
         newData = new Array(this.data.length + this.increment);
         for     (var i=0; i< this.data.length; i++) {
              newData[i] = this.data[i];
         this.data = newData;
    // trimToSize() -- trims the vector down to it's size
    function trimToSize() {
         var temp = new Array(this.getSize());
         for (var i = 0; i < this.getSize(); i++) {
              temp[i] = this.data[i];
         this.size = temp.length - 1;
         this.data = temp;
    // sort() - sorts the collection based on a field name - f
    function sort(f) {
         var i, j;
         var currentValue;
         var currentObj;
         var compareObj;
         var compareValue;
         for(i=1; i<this.getSize();i++) {
              currentObj = this.data[i];
              currentValue = currentObj[f];
              j= i-1;
              compareObj = this.data[j];
              compareValue = compareObj[f];
              while(j >=0 && compareValue > currentValue) {
                   this.data[j+1] = this.data[j];
                   j--;
                   if (j >=0) {
                        compareObj = this.data[j];
                        compareValue = compareObj[f];
              this.data[j+1] = currentObj;
    // clone() -- copies the contents of a Vector to another Vector returning the new Vector.
    function clone() {
         var newVector = new Vector(this.size);
         for (var i=0; i<this.size; i++) {
              newVector.addElement(this.data[i]);
         return newVector;
    // toString() -- returns a string rep. of the Vector
    function toString() {
         var str = "Vector Object properties:\n" +
         "Increment: " + this.increment + "\n" +
         "Size: " + this.size + "\n" +
         "Elements:\n";
         for (var i=0; i<getSize(); i++) {
              for (var prop in this.data[i]) {
                   var obj = this.data[i];
                   str += "\tObject." + prop + " = " + obj[prop] + "\n";
         return str;     
    // overwriteElementAt() - overwrites the element with an object at the specific index.
    function overwriteElementAt(obj, index) {
         this.data[index] = obj;
    Now I have written this code .This code is written using API'sfunction getOptions(ctx)
    var options="";
    var subroles = ctx.getAdminRole().getAttributeMultiValue("CUSTOM10");
    var v = new Vector(subroles);
    options = v.getElementAt(0);
    Now suppose the function getAttributeMultivalue is returning RoleA,RoleB
    Now i want RoleA to be stored in getElementAt(0) and RoleB in getElementAt(1)
    But with the above code i am getting RoleA,RoleB in getElementAt(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    abhishek20 wrote:
    well the question is also about Vector class of JavaNo it is not you doofus. It is about JavaScript. Your question has fuck all to do with Java.
    so I thought to post it here.That was a mistake.
    any ways if you don't know java script or anything about vector class then you should not reply.I know about JavaScript.
    I know about Vectors.
    I will not tell you anything about JavaScript here.
    I wouldn't tell you anything useful anyway because I have taken a personal dislike to you.
    >
    No need to make me understand what is the purpose of this forum.Really? Because you don't seem to get it. At all.
    next time take care before you reply.Next time engage your tiny pea brain in some sort of logical thought before posting.

  • Using a vector in Java IDL...URGENT!

    How can I use a vector in Java IDL? I am trying to have a function return a vector.
    From what I have read, I should use a sequence. Therefore I tried the following code:
         struct ItemDB {
              long ItemNumber;
              long Quantity;
              double Price;
    typedef sequence <ItemDB> ItemList;
         interface ProductOperations{          
              ItemList listItems();
    When the equivalent Java is generated for the interface I am forced to return an array instead. This is what is generated:
    public interface ProductOperationsOperations
    ItemDB[] listProducts ();
    However, this is an array, which is bounded. I want to use an unbounded storage device.
    Can someone please please assist me. I appreciate it.
    Thanks
    RG

    This is already an unbounded sequence
    typedef sequence <ItemDB> ItemList;
    Checkout the read/write methods in the Helper class and you can see that the above list is unbounded.

  • How do I count objects of a specific type in a Vector? Java 1.3 source.

    How do I count the objects of a specific type in a Vector?

    isInstance may allow too much for your needs as it allows any object which can be typecast to 'type' while you seem to want to limit to objects which are exactly of the type 'type'. In such a case you could do this:
    for (Enumeration e = attachmentTypes.elements(); e.hasMoreElements();)
      Object check = e.nextElement();
      if (check.getClass().equals(type)) {
        counter++;
    }Javadoc of the isInstance() method:
    http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#isInstance%28java.lang.Object%29
    (I link to the old Javadoc given you mentioned Java 1.3 compatible source)

  • Vector in java

    hi all
    i m having one vector and i need all elements of that vector except first element. can anyone help me?
    my vector is like
    [xyz,pqr,abc,mno]
    out of this vector i don't want vector element ' xyz'.
    how to do?

    import java.util.*;
    class test
         public static void main(String args[])
              Vector v=new Vector();
              //storing the element          
              v.addElement("xyz");
              v.addElement("pqr");
              v.addElement("abc");
              v.addElement("mno");
              //removing the element
              v.removeElementAt(0);
              Enumeration venum=v.elements();
              //printing the document
              while(venum.hasMoreElements())
                   System.out.print(venum.nextElement()+" ");
              System.out.println();
    }

  • Getting Data from C++ STL Vector into Java Vector

    Hi All,
    I have been involved in a project recently to port a lot of C++ code into Java. Testing the ported Java code has been done in isolation with JUnit and works fine, but we would like to test all the ported code. To do this we aim to call the ported (Java) methods from C++ using JNI.
    I have read a lot of the documentation from Sun on the JNI and can access and set Java methods that only contain primitive types without any problems. However what I cant seem to be able to achieve is passing aggregated C++ classes to Java.
    Java
    public void translation_unit_2( Vector v ) { \* code *\ }and somewhere in C++
    jobject actions;                  // Is initialized
    std::vector<Node> test ;   // Node class may contain other objects
    jmethodID tmp = env->GetMethodID(env->GetObjectClass(actions), "translation_unit_2", "(Ljava/util/Vector;)V");
    env->CallObjectMethod(actions, tmp, test);Now obviously what I have posted there doesnt work, but can anybody point me
    in the correct direction to pass the Vector and its Data in C++ to Java?
    None of the Java methods are native, because the eventually the whole system
    will be operating solely in Java, but the testing at this stage is quite important.
    If this is an impossible task then please say so also, any help will be appreciated !
    Thanks,
    Mark Hennessy

    Thanks for the reply and I understand that running an iterator
    over the vector would be the way the get the data out of the C++ Vector.
    However that still leaves me with the question of mapping an arbitrary
    C++ class ( which in the case of the example above would be a Node class which is
    composed of other aggregated classes ) to a jobject such that I can pass
    the data easily to Java throught JNI?
    Is this possible or would I have to decompose each and every class to its most
    primitive types and pass them over to Java and reconstruct them on the Java side?

  • Vectors in java

    I have created a vector inside vector
    Now my vector contains [one,two ,three]
    I want to display the contents of this vector in fashion
    two
    one
    three
    How can I do that?

    I use vectors bcos each time i run my application it has to store randomly generated data, whose length i cant pre-determine.ArrayList will adjust the length for you. Just use java.util.ArrayList instead of java.util.Vector. It will do the same thing, only faster (if you need the synchronization that Vector provides, there are methods in java.util.Collections to help you with that).
    May I now ask another question, I am looking for random number distribution generator in java. i could
    write one, but i figured i will just reuse. this will be different from the Math.random or rand() in java.
    I need it for a random exponential distribution.If nothing in java.util.Random works, then you could write your own, possibly using the output of those methods as a starting point. Otherwise, there are probably pre-existing Java exponential distribution classes on the web if you search for them.

  • What are vectors in java ?

    Dear forum users ,
    I would like to know if there be any resources available or any information available on what are vectors and how do they function in java . thank you .

    Dear forum users thanks again for your replies ,
    basically im trying to understand this below code which im not really sure whether its a vector or a for loop and if its neither what does it mean ,could anyone explain the part i have indicated below in bold especially the part on the for(int i=msgs.size()-1; i>=msgs.size()-maxText,i--) and the msg=(Message)msgs.elementAt( i ) ; and why the need for a declaration of a integer j,p=-1
    int maxText = 11; //max texts displayed
    if (history || msgs.size() < maxText)
    maxText = msgs.size();//all texts
    history = false; //reset
    int lin = 0; //links count
    *for (int i=msgs.size()-1; i>=msgs.size()-maxText; i- -) {*
    msg = (Message) msgs.elementAt( i );
    int j,p = -1;

  • Vector notation - Java vs Delphi

    Good morning everybody,
    On this beautiful day I've the opportunity to learn java. I have to upgrade an existing application that makes use of a class 'TreeElement' with a (vector) field 'childs' .
    public class TreeElement {
      private Vector childs;
      public Vector getChilds () { return childs; }
      public string label;
    }In the vector childs instances of 'TreeElement' are stored. In that way a tree is formed, but that's not the question at hand.
    I want to ask something about the java syntax. Is it necessary to write all this (especially the second part)?
    Vector layer = null;
    TreeElement element = null;
    String label = null;
    layer = tree.getChilds();
    element = (TreeElement) Layer.get(0);
    name = element.label;Firstly, a vector has to be defined to store the childs. Secondly a TreeElement has to be defined to store each child. Also a typecast seems to be necessary. And only now is it possible to access the method name.
    In Delphi this would be
    Name = tree.childs[0].label;And this can be written even shorter when making use of default properties:
    Name = tree[0].label;Is something alike possible in java?name = tree.getChilds().get(0).labelWhy not?

    With Java 1.5 you can write public class TreeElement {
      private List<TreeElement> children;
      public List<TreeElement> getChilds () { return children; }
      public String label;
    }and name = tree.getChilds().get(0).label

  • Passing vector between java class and jsp page

    Hi,
    I think this is similar to the last post but anyways maybe somebody can help us both out bontyh.....I am getting an error saying undefined variable or class when i try to access a Vector which was returned in a class.
    Anyways anyone got any ideas?

    Maybe if you give some more information...

  • Retuning a vector in java

    I want to return a vector from a method how do i do it. Please explain me with a suitable
    example. I'll be very gratefull.
    Its very urgent

    What's the input? if it's a string try this:
    public Vector tokens(String original,String divider) {
        Vector v = new Vector();
        StringTokenizer st = new StringTokenizer(original,divider);
        while(st.hasMoreTokens()) v.add(st.nextToken());
        return v;
    }Shish

  • Help needed to solve "java.io.NotSerializableException: java.util.Vector$1"

    hi to all,
    i am using a session less bean A to querry a Entity Bean B , which inturns calls another EntityBean C ,
    finally a ' find' method is invoked on the EntityBean C, In this a vector is created which holds 3 different vectors at different indexes, now the problem i am facing is that when Enity Bean B is returning the final vector to the A , it's firing out a errors that :-
    TRANSACTION COULD NOT BE COMPLETED: RemoteException occurred in server thread;
    ested exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.rmi.ServerException: RemoteException occurred in server thread; ne
    ted exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.ServerException: RemoteException occurred in server thread; nested exc
    ption is:
    java.rmi.RemoteException: null; nested exception is:
    java.rmi.ServerException: RemoteException occurred in server thread; ne
    ted exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.RemoteException: null; nested exception is:
    java.rmi.ServerException: RemoteException occurred in server thread; ne
    ted exception is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.ServerException: RemoteException occurred in server thread; nested exc
    ption is:
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.rmi.RemoteException: null; nested exception is:
    java.io.NotSerializableException: java.util.Vector$1
    java.io.NotSerializableException: java.util.Vector$1
    <<no stack trace available>>
    ur any help would be highly appricated to solve out this prob.
    If i try to iterate through this vector it's gives IOR:0232003x343242344asdsd................................................blabla....................
    thanxs in adavance
    Deepak

    Hi I think you are using the method elements() in a remote method.
    This method can't be Serializable!! Because it returns an Interface. Interfaces are never Serializable.
    Regards,
    Peter

  • Converting Java Vector to JavaScript Array

    Hi ,
    If any body know , plz let me Know ...
    I want to Convert Java Vector of Vectors to Java Script Array of Arrays ..Thanks in Advace ..
    SUBBA.

    Hi,
    I did something like this.I used hidden variables to pass all the info the client side and I called "onload" on the body tag.In this onload function call , i split that hidden variable into array of arrays.
    This needs to be done using proper delimiters.
    So the hidden variable wuold be of the form:
    V1E1#V1E2#V1E3#V1E4$$V2E1#V2E2#V2E3#$$
    V1E1 means Vector 1 Element 1.
    You need to form this properly on server and pass it to the client where it would be split onLoad and stores appropriately into arrays.
    thx.

  • Vector implementation in java

    Hi,
    I'm not too sure about the implementation of Vector in Java. Currently, this is what think:
    Vector.remove(0) - this is O(n), because you have to shift all the following elements up
    Vector.remove(size-1) - this is O(1), because you don't have to do anything else
    Is this correct?
    Thanks
    Feng

    Not quite. If Vector.remove(size - 1) leaves a
    sufficiently small proportion of the underlying array
    actually in use, it will resize, taking O(n) time.Here's the implementation of remove:
        public synchronized Object remove(int index) {
         modCount++;
         if (index >= elementCount)
             throw new ArrayIndexOutOfBoundsException(index);
         Object oldValue = elementData[index];
         int numMoved = elementCount - index - 1;
         if (numMoved > 0)
             System.arraycopy(elementData, index+1, elementData, index,
                        numMoved);
         elementData[--elementCount] = null; // Let gc do its work
         return oldValue;
        }So if index = elementCount - 1, then numMoved = (elementCount) - (elementCount-1) - 1 = 0.
    Thus, the OP is correct.

  • HELP?! a class that implements .....  : java.util.Vector

    My instructions say this:
    In the constructor, create the collection object (Since java.util.List is an interface, you will need to instantiate a class that implements this interface: java.util.Vector or java.util.LinkedList or java.util.ArrayList).
    I know, this IS a homework assignment - I am not sure how to go about this. I DO have code so far up to this point, can anyone help?
    import dLibrary.*;
    import java.awt.Color;
    * @author Rob
    * @version 8/18/03
    public class PhrasesII extends A3ButtonHandler
            private A3ButtonWindow win;
            private ATextField stringAcceptor;
            private ALabel listOstrings;
            private ALabel inputString;
            private ALabel status;
            private java.awt.List display;
            private java.util.List collection;
    public PhrasesII()
            win = new A3ButtonWindow(this);
            stringAcceptor= new ATextField (50,420,200,25);
            stringAcceptor.place(win);
            listOstrings = new ALabel(100, 10, 100, 380);
            listOstrings.setFontSize(10);
            listOstrings.setText("List of Strings:");
            listOstrings.place(win);
            inputString = new ALabel(50,400,200,25);
            inputString.setFontSize(10);
            inputString.setText("Input String:");
            inputString.place(win);
            display = new java.awt.List();
            display.setLocation(200, 100);
            display.setSize(200, 250);
            display.setBackground(Color.lightGray);
            win.add(display, 0);
            win.setLeftText("Save");
            win.setMidText("Display");
            win.setRightText("Discard");
            win.repaint();
      public void leftAction()
        public void midAction()
        public void rightAction()

    I am getting a " can't resolve symbol" when I do thisYou have to either import java.util.ArrayList or specify it fully, e.g., "new java.util.ArrayList()".
    Is that the line that is causing you problems? The error message should give the line number.
    my instructions also say I have to use "interface
    java.util.List when declaring your reference" so I am
    confused about using "= new ArrayList();"What they're saying is that you want code like this:
    private java.util.List frogs;    // this is the reference declaration
    //... later on...
    frogs = new java.util.ArrayList();  // this isn't a declarationWhat this means is that when you declare a field or variable, you should declare its type to be an interface.
    But when you actually instantiate a value for that variable, then you should use a concrete class that implements that interface. (You have to; interfaces can't be instantiated.)
    This is good programming style for reasons I don't have the space to explain here.

Maybe you are looking for