Results 1 to 4 of 4
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default How can i remove a value from an array?

    i have a program with several methods. it inserts about 10 elements.

    i find the maxvalue with this method

    Java Code:
    public long getMax()
    	{
    		if (nElems != 0)
    		{
    			long maxValue = a[0];
    			for (int index = 1; index < a.length; index++)
    			{
    				if (a[index] > maxValue)
    				{
    					maxValue = a[index];
    				}
    			}
    			return maxValue;
    		}
    		else
    		{
    			return -1;
    		}
       	}
    with this call
    Java Code:
     maxValue = arr.getMax();
          System.out.println("The highest value in the array is " + maxValue);
    i need to make a remove method that takes that maxvalue and removes it. but i cant figure out how to do it.

  2. #2
    tanjo3 is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Traverse the array again and when it finds the highest value set it to zero?
    Java Code:
    public void removeMax()
    {
    	for (int i = 0; i < a.length; i++)
    	{
    		if (a[i] == maxValue)
    			a[i] = 0;
    	}
    }
    Is that what you want?

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    idk. it doesnt work because it cant find maxValue. and when i try to fiddle around i keep getting missing identifier.

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

    Default

    Quote Originally Posted by Meta View Post
    idk. it doesnt work because it cant find maxValue. and when i try to fiddle around i keep getting missing identifier.

    if you store your values in an collection of type list then this class offers a remove(index) method.
    Last edited by j2me64; 01-31-2011 at 11:06 AM.

Similar Threads

  1. Remove .0
    By maple_leafs182 in forum New To Java
    Replies: 4
    Last Post: 01-28-2011, 02:47 AM
  2. How do I remove objects from an Array in java?
    By hamed in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 01:20 PM
  3. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 02:26 PM
  4. Replies: 1
    Last Post: 08-01-2007, 08:55 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
  •