Results 1 to 7 of 7
Thread: Vector understanding
- 05-03-2010, 04:03 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
Vector understanding
Hi, still new on java, just reading on vectors and understand the basics. However I'm confused on how using vectors in methods..
Say I have a Student class here
public class Student extends Person {
private String schoolYear;
private int mark;
public Student(String n, String dofb, String y, int m) {
super(n,dofb);
schoolYear = y;
mark = m;
}
public String getschoolYear() {
return schoolYear;
}
public int getMark() {
return mark;
}
}
Now say I stored these objects in a vector which is in the main method. In a different Class say I wanted to make a findMark method which as parameters take the vector and a name. What the method would do is check the parameter name and put it against the name variables of the objects in the vector then from that object take the mark and return it.
The only confusion for this at the moment I have is when I take the vector as a parameter:
import java.util.*;
public class finding {
public int findMark(Vector v, String name) {
What I'm confused on is how to store the contents of the vector locally in this method. Like for the name I would usally just make a local string variable and store the parameter name in there but I dont know how to do this for the vector.
So at the moment I have this which just locally stores the parameter in the method:
import java.util.*;
public class finding {
public int findMark(Vector v, String name) {
String n = name;
I would like to know how to do the same for the vector parameter.
Any help of examples for this would be much appreciated, thanksLast edited by counterfox; 05-03-2010 at 04:08 PM.
- 05-03-2010, 04:11 PM #2
??Java Code:import java.util.*; public class finding { Vector vec = new Vector(); public int findMark(Vector v, String name) { String n = name; vec = v;
you need an empty vector to fill i believe:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 05-03-2010, 04:14 PM #3
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 05-03-2010, 04:16 PM #4
you have to create the same type of vector like in the main method inside your finding class, example Vector<Student> vec = new ...
But for a better design i would create a class example MyStudents with the name of the school and that hold the number of students and have methods like addStudent, delStudent that do all stuff like finding and so on.Last edited by j2me64; 05-03-2010 at 04:19 PM.
- 05-03-2010, 04:52 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
thanks I managed to get the vector stored locally and I tried to do the rest but having a problem..
What this method does now is uses the parameter string and if the "name" is a name of a object int he vector.. should take the mark of that object and return it.. This is my attempt so far but with no success:
import java.util.*;
public class Ex6 {
public static int findMark(Vector v, String name) {
int mark=0; //Return value
int key=0; //Will contain the index of object were looking for
String n = name; //locally contains parameter name
Vector vec = new Vector(); //create local vector
vec = v; //puts parameter objects in local vector
//here i wanted to check if the vector has the object needed and then get the index of it
if(vec.contains(name)){
key = vec.indexOf(name);
}
//From that it would take the object from the vector and store it locally in the method(from casting)
Student ans =(Student) vec.elementAt(key);
//Then it would use a getter method from the Student class to get the mark and return it
mark = ans.getMark();
I thought for a basic start might work. It does compile (only because I initialised the local variables with 0) The problem is I tested this in a main method and it always returns the mark of index 0, not ever even index,1,2 etc from when I change the main method
Can someone point me in the right direction please?
- 05-03-2010, 05:02 PM #6
Huh?!
And use code tags when posting code.Java Code:public static int findMark(Vector<Student> v, String name) { int mark=-1; //Return value for (Student s : v) { if(s.name.equals(name)){ return s.mark; } } return mark; }Last edited by PhHein; 05-03-2010 at 05:06 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 05-04-2010, 10:59 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
need help understanding part of code
By mustachMan in forum New To JavaReplies: 7Last Post: 02-09-2011, 07:11 PM -
need help in understanding collection
By ShinTec in forum Advanced JavaReplies: 2Last Post: 04-24-2010, 02:49 AM -
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
Help on understanding a program
By newbie225 in forum New To JavaReplies: 1Last Post: 11-10-2009, 12:53 AM -
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11-05-2007, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks