Results 1 to 7 of 7
Thread: Need Help Regarding Vectors
- 05-23-2010, 06:24 AM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
[SOLVED]Need Help Regarding Vectors
Hi guys, I'm new to this forums and currently doing a task by my school.
So far so good, but there's something I can't figure it out, it's like this,
I can't seem to get my vector values to be stored in a dummy Object at this line IdolFinalist temp = vec.get(i);Java Code:import java.util.*; public class TestVector3 { public static void main(String[] args) { IdolFinalist kathy = new IdolFinalist("Kathy Tan", 200); IdolFinalist nadia = new IdolFinalist("Nadia Lim", 350); IdolFinalist dan = new IdolFinalist("Dan Osman", 150); Vector v = new Vector(); v.add(kathy); v.add(nadia); v.add(dan); System.out.println(v.get(1)); for(int i = 0; i<v.size(); i++) { IdolFinalist temp = v.get(i); System.out.println(temp.getName() + temp.getVotes()); } } }
Any help will be appreciated thanks.Last edited by xHoly; 05-23-2010 at 06:57 AM.
- 05-23-2010, 06:30 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
The get() method needs an int index -- you need to tell it which element of the Vector you want to get. (Hint: you probably want your i variable.)Java Code:IdolFinalist temp = v.get();
-Gary-
- 05-23-2010, 06:33 AM #3
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
- 05-23-2010, 06:35 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Where do you get incompatible type? Show us the exact code you are trying to compile and run, and give us your exact error message please.
-Gary-
- 05-23-2010, 06:38 AM #5
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Java Code:public class IdolFinalist { private String name; private int votes; public IdolFinalist() { name = null; votes = 0; } public IdolFinalist(String n, int v) { name = n; votes = v; } public String getName() { return name; } public void setName(String n) { name = n; } public int getVotes() { return votes; } public void setVotes(int v) { votes = v; } }I got incompatabile type at line 26 which isJava Code:import java.util.*; public class TestVector3 { public static void main(String[] args) { IdolFinalist kathy = new IdolFinalist("Kathy Tan", 200); IdolFinalist nadia = new IdolFinalist("Nadia Lim", 350); IdolFinalist dan = new IdolFinalist("Dan Osman", 150); Vector v = new Vector(); v.add(kathy); v.add(nadia); v.add(dan); System.out.println(v.get(1)); for(int i = 0; i<v.size(); i++) { IdolFinalist temp = v.get(i); System.out.println(temp.getName() + temp.getVotes()); } } }Java Code:IdolFinalist temp = v.get(i);
- 05-23-2010, 06:47 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I see it now. You are using a raw Vector, which always stores objects of type Object. You should either use a generic Vector<IdolFinalist> or you need to cast the object on your get() call. So either this:
or this:Java Code:Vector<IdolFinalist> v = new Vector<IdolFinalist>();
-Gary-Java Code:IdolFinalist temp = (IdolFinalist)v.get(i);
- 05-23-2010, 06:56 AM #7
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Vectors Problem
By dashwall in forum New To JavaReplies: 9Last Post: 01-04-2010, 11:16 PM -
[SOLVED] Vectors
By porchrat in forum New To JavaReplies: 5Last Post: 05-26-2009, 02:11 PM -
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM -
Help with Vectors and Strings...
By kaban in forum New To JavaReplies: 2Last Post: 12-09-2007, 09:04 AM -
Vectors vs ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 10:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks