Results 1 to 15 of 15

Thread: Vector Problem

  1. #1
    osmaavenro is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default Vector Problem

    I am implementing a searching method for my first program using Vectors

    But it does not work not sure why??



    The program runs but the search is unsuccessful.

    Thank you for time and interest.

    Java Code:
    import java.util.*;
    public class test {
    
    	
    	public static void main(String[] args) {
    		Customer dan = new Customer("dan rogers","13 apple road",8787);
    		Customer carl = new Customer("carl","17 peach road",89);
    		Customer michael = new Customer("michael ford","345 apple road");
    		
    		Vector<Customer> cu = new Vector<Customer>();
    		cu.add(dan);
    		cu.add(carl);
    		cu.add(michael);
    				
    		
    		Scanner sc = new Scanner(System.in);
    		String c = sc.nextLine();
    		
    		
    			
    		searchVector(cu,c);
    		
    		
    			
    		
    	    
    	
    		
    	}//end main
    
    	// Search Method
    
    	public static void searchVector(Vector<Customer> v, String c ){
    		int i = v.indexOf(c);
    		if(i >=0){
    			System.out.println("Found "+ i);
    			System.out.println();
    			Customer temp  = v.get(i);
    			System.out.printf("%s\t %s\t %s\t",temp.getName(),temp.getTelephone(),temp.getAddress());
    			System.out.println();
    			}
    		else {
    			System.out.println( c +" Does not exist");
    		}
    		
    	} Search Method
    	
    
    }// end class

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    indexOf() does not do what you think it does.

    Read that link to the API for it.

  3. #3
    osmaavenro is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi there Thank you for the quick reply

    i have read the definition indexof().

    This is the first time i use vectors so i am quite confused now.


    i guess i need to use the equal to , and then get the index ??

    not sure if my logic is going in the right direction.

    is there better way to do the search. ???

    Thank you for time and interest

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can use indexOf, but it won't work if your Vector is a Vector of Customers, i.e., Vector<Customer>, and you call indexOf with a String parameter, not a Customer parameter. Otherwise if you must use a String, then you'll probably have to loop through the Vector checking each Customer to see if the Customer's (?) name field matches the String.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    osmaavenro, thanks for having us waste our time answering a question that has already been answered elsewhere.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    You can use indexOf, but it won't work if your Vector is a Vector of Customers, i.e., Vector<Customer>, and you call indexOf with a String parameter, not a Customer parameter.
    I'm not really awake yet (waiting for my espresso machine to do its job) but shouldn't Sun have used generics for the indexOf( ... ) method and its compadres? A mistake like the OP made is easily made and there's nothing that protects him/her ... hm, I have to check this ...

    kind regards,

    Jos

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I was wondering that...not for long, admittedly, but I did wonder.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    I was wondering that...not for long, admittedly, but I did wonder.
    I had my espresso now and I'm completely awake *ping!* The Vector class uses the equals( ... ) method in its indexOf( ... ) method of the components and that method is free to do what it wants (no matter what paper contract) so the indexOf method should accept any type of object. I still find it a bit of a weakness though ...

    kind regards,

    Jos

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Yes, equals can take whatever, but surely that doesn't require that indexOf() has to?

    Not that I've ever written a generic thingy (well, not since Ada anyway), so maybe I'm missing something.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Yes, equals can take whatever, but surely that doesn't require that indexOf() has to?
    But the indexOf( ... ) method uses the equals( ... ) method of the objects in the vector itself; the latter method can take everything as a parameter and the former simply passes it around:

    Stolen from the src.zip (Java 1.6.20)
    Java Code:
    public synchronized int indexOf(Object o, int index) {
    	if (o == null) {
    	    for (int i = index ; i < elementCount ; i++)
    		if (elementData[i]==null)
    		    return i;
    	} else {
    	    for (int i = index ; i < elementCount ; i++)
    		if (o.equals(elementData[i])) // <--- here
    		    return i;
    	}
    	return -1;
        }
    kind regards,

    Jos

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    But why would this be wrong:
    Java Code:
    public synchronized int indexOf(E e, int index) {
    ...etc etc.
    }
    (Apologies for any gross genericism syntax problems)

    Or is this going to be something to do with type erasure?

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    But why would this be wrong:
    Java Code:
    public synchronized int indexOf(E e, int index) {
    ...etc etc.
    }
    (Apologies for any gross genericism syntax problems)

    Or is this going to be something to do with type erasure?
    It's legacy (mostly); the Vector class already existed before generics saw the light but they even went that far as to defining the indexOf( ... ) method in the List interface to accept the same (Object type) (because the Vector class was retrofitted to match the List interface). Allowing both Objects and generic types E to be searched for is impossible (not quite sure about this yet); it would need method overloading which would be useless here ...

    kind regards,

    Jos

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Except that type erasure means that shouldn't cause a problem surely?
    And if you have legacy code that doesn't define what type the Vector is (ie pre-generics) then 'E' would be an Object anyway.

    I must be missing something here...

    I mean, add(E) exists, when it was add(Object) originally, so why is indexOf() any different?

    ETA: Ah, except I see that contains() is Object, as is remove()...so there must be something to do with (as you say) searching.

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    ETA: Ah, except I see that contains() is Object, as is remove()...so there must be something to do with (as you say) searching.
    Yep, the object passed in the indexOf( ... ) method doesn't have to be an E (genericalized speaking) because the equals( ... ) method of that parameter is to decided whether or not two objects are considered equal (see the stolen code example). The equals( ... ) method isn't a generic method because it is defined in the Object class and generifying that class means that every class needs to be generified. It's a design issue really (or a 'weakness' of the generics stuff if you want ;-)

    kind regards,

    Jos (<--- dislikes legacy code ;-)

Similar Threads

  1. vector problem
    By shinichi90 in forum New To Java
    Replies: 9
    Last Post: 08-30-2010, 09:25 PM
  2. Problem with Vector
    By banhbaochay in forum New To Java
    Replies: 9
    Last Post: 04-01-2010, 06:01 PM
  3. Problem with vector
    By morghul in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 03:24 AM
  4. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 09:53 PM
  5. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 11:44 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
  •