Results 1 to 5 of 5
  1. #1
    evapisces is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Question Passing a Vector object to a function

    Hi:
    I am working on a program that involves a string Vector object. I have to re-write the selection sort procedure using the vector object (instead of an array), and then re-write the binary search procedure also using the vector object. I already have the code written for an array for both procedures, but I'm just not sure how to pass the Vector object to my sort and search functions so that I can re-work the functions for the Vector. I've looked through multiple Java books I have, but none of them are helping, so I thought I'd post my problem here.

    Any help would be greatly appreciated. If you need more info from me, just let me know and I'll post it.

    Thanks in advance

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

    Default

    Given the information you've provided, your guess is as good as ours. You may wish to provide more details, and in fact repost the question as if we can't read your mind or magically see your code.

  3. #3
    evapisces is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Sorry I'm not the best explainer. Here's the code I have so far:

    Java Code:
    import java.util.*;
    
    public class VectorOperations
    {
        static Scanner console = new Scanner(System.in);
    	 
    	 public static void main(String[] args)
    	 {
    	     Vector<String> stringList = new Vector<String>();
    		  int ans;
    		  String target;
    		  int index;
    		  
    		  do
    		  {
    		      System.out.println("Enter a string to be added: ");
    		      stringList.addElement(console.next());
    				
    				System.out.println("Would you like to enter another string?\n [ Enter (1) for YES and (2) for NO ]");
    				ans = console.nextInt();
    		  } while (ans == 1);
    		  
    //		  selectionSort(stringlist);
    //		  System.out.println(stringList);	   
    
    		  
    		  System.out.println("Enter the item you would like to search for: ");
    		  target = console.next();
    		  
    //		  index = binarySearch(stringList, target);
    //      System.out.println("The string " + target + " was found at position: " + index);
    		  
    	 }
    	 
    
    	 
    //	 public void selectionSort()
    //  {
    
    
    //  }
    
    //	 public int binarySearch()
    //  {
    
    
    //  }
    
    }
    I commented out the two functions down at the bottom, and the lines commented out in MAIN are operations I can't do yet because I haven't written the sort and search functions yet.

    So I just wanted to know if anyone knows a snytax for passing a Vector object to a function because I'm not familiar at all with Vectors, I've been using arrays for most of my programming life.

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

    Default

    You declare a Vector parameter the same way that you pass any parameter: class name followed by a parameter variable:
    Java Code:
    public void myFoo(Vector<String> stringVector)
    {
      //....
    }

  5. #5
    evapisces is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Oh, okay that makes sense. I guess I wasn't thinking clearly because Vectors are completely new to me. Thanks for your help. :)

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Search a object in a vector
    By TalhaS in forum New To Java
    Replies: 2
    Last Post: 04-30-2008, 02:05 PM
  3. Problems passing a scan object to a method
    By xkross in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 03:56 PM
  4. URGENT: Sorting a vector of object by an element
    By doobybug in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 06:37 PM
  5. Replies: 2
    Last Post: 07-09-2007, 05:01 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
  •