Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 02:03 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 07:56 AM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
You need to know the index position of the element in the Vector.
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.
Code:
System.out.println((String) vector.get(vector.size()-1));
To know more about the available methods of Vector, read this
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vectors of Vectors or hash-somethings? mindwarp New To Java 3 03-10-2008 04:57 PM
Sorting JTable (Vectors) Problem ramapple AWT / Swing 5 02-05-2008 10:54 AM
problem with Vectors and getTotal() function java_fun2007 New To Java 2 11-23-2007 03:55 PM
Understanding Vectors cbrown08 New To Java 7 11-05-2007 08:56 PM
Using Vectors in Java JavaForums Java Blogs 0 11-04-2007 09:31 PM


All times are GMT +3. The time now is 11:14 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org