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

Thread: Vectors

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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 11:00 AM.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Vectors

    Very nice tutorial, chili5! +rep

  3. #3
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: Vectors

    Very interesting !

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Vectors

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

  5. #5
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  6. #6
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  7. #7
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  8. #8
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #9
    Guru mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim.'s Avatar
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,393

    Re: Vectors

    Nice Tutorial , +rep .!

  10. #10
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

    Re: Vectors

    Nice Job .. +rep

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Array Sorting Algorithms II
    By whitey6993 in forum C Tutorials
    Replies: 4
    Last Post: 12-30-2008, 12:26 PM
  2. Array Sorting Algorithms I
    By whitey6993 in forum C Tutorials
    Replies: 2
    Last Post: 12-30-2008, 11:24 AM
  3. Creating vectors in objects?
    By MerakSpielman in forum C and C++
    Replies: 2
    Last Post: 03-30-2008, 05:26 AM
  4. Vectors functions
    By JohnSmith in forum C and C++
    Replies: 4
    Last Post: 03-06-2008, 11:48 PM
  5. Help with Arrays and Vectors!!!
    By PrettyVacant in forum C and C++
    Replies: 2
    Last Post: 11-29-2007, 09:56 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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