+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Vectors

  1. #1
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Vectors

    Vectors in Java

    A Vector is a container that expands in size as new data is added. The Vector container cannot hold primitive variables. In can only hold objects. Therefore, if you wish to include a primitive data type in a vector you must use it's wrapper class.

    Table 1.1 Primitive Wrapper Classes



    By using the new operator with any of the wrapper classes, you can add a primitive variable to the vector.

    For example:
    Code:
    Integer nPrime = new Integer(5);
    Boolean bPrime = new Boolean(false);
    Making a Vector

    You must first import the java.util.Vector class.


    Constructors


    The Vector class contains four constructors:

    Code:
    Vector();
    Vector(Collection coData);
    Vector(int nSize);
    Vector(int nSize, int nCapacity);
    If you use the first constructor, an empty vector is created with a default size of 10, and it's capacity increment is zero.
    The second Vector constructor takes a Collection and populates the vector with the elements in the collection.
    The third Vector constructor takes a single integer that specifies the beginning size of the constructor.
    The forth constructor takes a integer specifiy the beginning size of the constructor and a second integer that specifies the increment.

    Adding to a Vector

    First create a Vector object. We will populate this vector with information about 3 people. This will include the following data:

    1. Name (String)
    2. Age (Integer)
    3. Gender (Boolean - true represents male and false represents female)
    4. Middle Initial (Character)


    The data we want to add to the vector is this:

    Table 1.2 Example data


    Since we can only have objects added to our vector, we need to create a String object, an Integer object, a Boolean object, and a Character object. I am going to pass the four objects to a method that illustrates how to return a Vector.

    Code:
    public class VectorTest {
    
        public static void main(String[] args) {
            Vector vPerson = new Vector(); // a vector that has a size of 10
    
            String sName;
            Integer nAge;
            Boolean bGender;
            Character cInitial;
            
            sName = "James"; // notice that there isn't a new String("James") line here. This isn't required, as Java has created a short form for assigning Strings. However I could have done sName = new String("James");
            nAge = new Integer(16);
            bGender = new Boolean(true);
            cInitial = new Character('G');
            
            vPerson = addElements(vPerson, sName, nAge, bGender, cInitial);
            
            
            sName = "Harry";
            nAge = new Integer(25000);
            bGender = new Boolean(false);
            cInitial = new Character('R');
            
            vPerson = addElements(vPerson, sName, nAge, bGender, cInitial);
            
            sName = "Flipper";
            nAge = new Integer(97400);
            bGender = new Boolean(true);
            cInitial = new Character('A');
            
            vPerson = addElements(vPerson, sName, nAge, bGender, cInitial);
        }
        
        public static Vector addElements(Vector vPerson, String sName, Integer nAge, Boolean bGender, Character cInitial) {
            vPerson.add(sName);
            vPerson.add(nAge);
            vPerson.add(bGender);
            vPerson.add(cInitial);
            
            return vPerson;
        }
        
    }
    The .add() method of the vector adds the specified element to the next space available in the Vector. I pass to a method the Vector, and the four elements that I wish to add to the Vector. The method adds the four elements to the vector, and returns the modified Vector.

    Now to output the results, I create a List iterator and four temporary primitive variables to hold the results.

    Like so:

    Code:
    ListIterator iterator = vPerson.listIterator();
    String sTempName;
    int nTempAge;
    boolean bTempGender;
    char cTempInitial;
    
     while (iterator.hasNext()) {
                sTempName = iterator.next().toString();
                nTempAge = Integer.parseInt(iterator.next().toString());
                bTempGender = (Boolean)iterator.next();
                cTempInitial = (iterator.next().toString().charAt(0));
                System.out.print(sTempName + " " + cTempInitial + " is " + nTempAge + " years old,");
                if (bTempGender) {
                    System.out.print(" and is a male.");
                } else {
                    System.out.print(" and is a female.");
                }
                System.out.println();
            }
    The iterator returns an Object, which I then convert to a String and using the wrapper class converting into back into the primitive data type.

    This code produces this output:

    James G is 16 years old, and is a male.
    Harry R is 25000 years old, and is a female.
    Flipper A is 97400 years old, and is a male.
    I've just covered adding elements to the Vector but you can remove elements from the Vector.

    Please note, that this is just an example. In an ideal situation, you would create a Person class. In which you could add Person objects to the Vector.

    Hope this was helpful!

    Enjoy,
    James

    External resources:

    Java: Vectors
    Vector (Java 2 Platform SE v1.4.2)
    Last edited by chili5; 02-16-2009 at 09:00 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Vectors

    Very nice tutorial, chili5! +rep

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Vectors

    Very interesting !

  5. #4
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: Vectors

    Nice. I've always found Java's insistence on wrapper classes to access primitives annoying, though.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Vectors

    Quote Originally Posted by chili5
    James CC
    Did you change your surname?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Vectors

    lol that was a typo.

    @winged - Do you know why sun decided we have to use wrappers and not primitives? I find wrappers annoying also.

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Vectors

    Sure it was.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: Vectors

    Java views classes as being a different type of data than primitives. This is different from C++'s perspective that classes and primitives should be equivalent. I think it's about Java's desire to be pure OOP, but still having to support primitives.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,391
    Rep Power
    30

    Re: Vectors

    Nice Tutorial , +rep .!

  11. #10
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Vectors
    By vasil_9x in forum C and C++
    Replies: 1
    Last Post: 03-07-2011, 11:18 AM
  2. C++ Vectors
    By Hunter100 in forum C and C++
    Replies: 8
    Last Post: 06-27-2010, 09:50 PM
  3. More About Vectors
    By chili5 in forum Java Tutorials
    Replies: 5
    Last Post: 06-14-2010, 05:05 AM
  4. Understanding vectors
    By Walle in forum C and C++
    Replies: 6
    Last Post: 01-18-2010, 12:33 PM
  5. Help with Arrays and Vectors!!!
    By PrettyVacant in forum C and C++
    Replies: 2
    Last Post: 11-29-2007, 07:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts