Results 1 to 4 of 4
  1. #1
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default Vector+remove function

    Hello!
    Working on my vector class atm,encountered a problem and dont rly know how to proceed.

    Function declaration in the Interface file:
    Java Code:
    boolean remove(int pos);
    Implementation in the Vector class:

    Java Code:
    public boolean remove(Object elem) 
    {
    		boolean flag=false;
    		
    		
    		for(int i=0;i<capacity;i++)
    		{
    			if(elem.equals(this.elements[i]))
    			{
    				this.elements[i]=null;
    				flag=true;	
    			}
    		}
    		this.nrofelements--;
    		return flag;
    }
    in my testfile i use my add function to add a number to the Vector,and then trying to remove it.
    Java Code:
    public class TestVector 
    {
    	public static void main(String[] args)
    	{
    		Vector<Object> v = new Vector<Object>();
    
    		v.add(666);
    		if(v.remove(666)==true)
    		{
    			System.out.println("Item deleted");
    		}
    		else
    		{
    			System.out.println("Item not found");
    		}
    		
    	}
    }
    this is the error msg im getting:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 666
    at Vector.remove(Vector.java:92)
    at TestVector.main(TestVector.java:10)
    Hope some1 can point out the error in my code,thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Have you read the API doc for the Vector class?
    What does the remove(int) method do?

    What does the add() method do?
    If you're not sure what is being added to the Vector, print out the contents of the Vector to see what was added by this:
    add(666);
    Java Code:
          for(int i=0; i<v.size(); i++)
            System.out.println("v[i] class = " + v.get(i).getClass());  // v[i] class = class java.lang.Integer
    This is an example of what the new compiler does to unexpecting programmers. The technique is called autoboxing. A sometimes gotcha for lazy programmers.
    Last edited by Norm; 05-27-2011 at 05:25 PM.

  3. #3
    w95
    w95 is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default

    Well,the vector class im writing is generic,afaik i dont need to use any "wrappers" when im adding the elements.Ive implemented a method that returns the object at the position i specify,and it seems like my v.add works as it should.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    it seems like my v.add works as it should.
    What do you expect to be added to the Vector?

    Ive implemented a method that returns the object at the position i specify
    How do you determine the position of an object? By position I assume you mean an index start at 0 going to #items - 1

Similar Threads

  1. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  2. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  3. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  4. Passing a Vector object to a function
    By evapisces in forum New To Java
    Replies: 4
    Last Post: 09-27-2008, 03:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •