Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Problem with vectors in java

    Hi, Here's my problem, I want to use vectors, in this random example i have created an object car, with a firstname of fred (told you it was random).
    My question is how can i put in the System.out.println statement the code to get fred printed? I want to get the name of the car via the vector somehow. Not just car.firstName, anyone know how i would do it? I thought it would be vector.firstname.get(1) but doesn't work.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class veh{
    
    public static void main(String[] args) {
    Contact temp = new Contact();
    Vector vector = new Vector();
    Contact car = new Contact();
    car.firstName = "fred";
    vector.addElement(car);
    System.out.println(?????);
    
    }
    }
    Thanks

  2. #2
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default

    You need to know the index position of the element in the Vector.
    Java Code:
    System.out.println((String) vector.get(index));
    Since you use addElement to add the element in the Vector, it adds it at the end of the Vector.
    You can then determine the index by getting the size of the vector.
    Java Code:
    System.out.println((String) vector.get(vector.size()-1));
    To know more about the available methods of Vector, read this
    Greetings.

Similar Threads

  1. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-06-2009, 11:15 PM
  2. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM
  3. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 01:55 PM
  4. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 PM

Posting Permissions

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