Results 1 to 7 of 7
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default 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, thanks
    Last edited by counterfox; 05-03-2010 at 04:08 PM.

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    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 ~-'

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by Sno View Post
    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
    Nope, he wants to return an int, for whatever that's good for.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by counterfox View Post
    I would like to know how to do the same for the vector parameter.

    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.

  5. #5
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    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?

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Huh?!
    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;
    }
    And use code tags when posting code.
    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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Is there where I point out that (outside of J2ME I believe) you should be using ArrayList rather than Vector?

    ...and in this case I'd say a Map of some sort, to be honest.

Similar Threads

  1. need help understanding part of code
    By mustachMan in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 07:11 PM
  2. need help in understanding collection
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2010, 02:49 AM
  3. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  4. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 AM
  5. 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
  •