Link to home
Start Free TrialLog in
Avatar of jtcy
jtcy

asked on

Vector problem

I need to return the minimum and maximum INDEX of a vector:

      public static int minimum(Vector costs)
       {
          int min = ((Integer) costs.get ( 0 ) ).intValue () ;
          for ( int i = 1, iSize = costs.size () ; i < iSize ; i ++ )
           {
                 Integer temp = ( Integer ) costs.get ( i ) ;
                 int tempVal = temp.intValue () ;

                 if ( tempVal < min )
                  min = i ;

           }    
          return min;

     }
     
      public static int maximum(Vector costs)
       {
          int max = ((Integer) costs.get ( 0 ) ).intValue () ;
          for ( int i = 1, iSize = costs.size () ; i < iSize ; i ++ )
           {
                 Integer temp = ( Integer ) costs.get ( i ) ;
                 int tempVal = temp.intValue () ;

                 if ( tempVal > max )
                  max = i ;

           }    
          return max;

     }
       
 
the minimum works but not the maximum~
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

      public static int maximum(Vector costs) {
            int max = Integer.MIN_VALUE;
            for (int i = 0, i < costs.size(); i++) {
                  Integer temp = (Integer) costs.get(i);
                  int tempVal = temp.intValue();
                  max = Math.max(max, tempValue);
            }
            return max;
      }

>>max = Math.max(max, tempValue);

should have been

max = Math.max(max, tempVal);
Avatar of jtcy
jtcy

ASKER

Thanks but I need to return the maximum INDEX, not value.
Here's a general solution that will work with most any Vector where the class of all of the elements are the same, and implement comparable (true for your Integers, as well as Strings) --

import java.util.Vector;

public class VectorMinMax {

      int minIndex = -1;
      int maxIndex = -1;
      
      public VectorMinMax(Vector v) {

            if (v.size() == 0)
                  return;

            Object minObject = v.get(0);
            Object maxObject = v.get(0);
            
            for (int i = 0; i < v.size(); i++) {
                  Comparable c = (Comparable) v.get(i);
                  if (c.compareTo(minObject) < 0) {
                        minObject = c;
                        minIndex = i;
                  }
                  if (c.compareTo(maxObject) > 0) {
                        maxObject = c;
                        maxIndex = i;
                  }
            }
      }

      public int getMinIndex() {
            return minIndex;
      }
      
      public int getMaxIndex() {
            return maxIndex;
      }
      
      public static void main(String[] args) {
            Vector v = new Vector();
            v.add("One");
            v.add("Two");
            v.add("Alpha");
            v.add("Beta");
            VectorMinMax vmm = new VectorMinMax(v);
            System.out.println("Min: " + v.get(vmm.getMinIndex()) + " at " + vmm.getMinIndex());
            System.out.println("Max: " + v.get(vmm.getMaxIndex()) + " at " + vmm.getMaxIndex());
            
            System.out.println();
            
            v = new Vector();
            v.add(new Integer(100));
            v.add(new Integer(-10));
            v.add(new Integer(1));
            v.add(new Integer(400));
            v.add(new Integer(10000));
            vmm = new VectorMinMax(v);
            System.out.println("Min: " + v.get(vmm.getMinIndex()) + " at " + vmm.getMinIndex());
            System.out.println("Max: " + v.get(vmm.getMaxIndex()) + " at " + vmm.getMaxIndex());
      }
}


And here's the run:

Min: Alpha at 2
Max: Two at 1

Min: -10 at 1
Max: 10000 at 4
So bascially you would take your vector costs and just go

VectorMinMax vmm = VectorMinMax(costs);
int min = vmm.getMinIndex();
int max = vmm.getMaxIndex();

Etc.

If you want to go whole hog, you could extend Vector and override get(), add(), etc., and keep the min/max as you add/remove elements from your Vector, which would be pretty neat, and a lot more efficient.
>>Thanks but I need to return the maximum INDEX, not value.

I don't know what you mean by that. If you mean as follows

index 0 - value 3948593485
index 1 - value 3948579
index 2 - value 928759

then the maximum index is given by

int maxIndex = v.size() - 1;
He means:

What is the index of the max value?
What is the index of the min value?
Oh I see! You mean the index of the maximum value. That simply is the following - using my original code

int indexOfMaximumValue = v.indexOf(new Integer(maximum(v)));
And of course if you sort the Vector:

int indexOfMinimumValue =  0;
int indexOfMaximumValue = v.size() - 1;
jtcy -- one thing you should decide is what the "max index" is when more than one Vector element have the same value.
So the simplest thing to do if you don't want to disturb the sort order is to do:


Vector v2 = (Vector)v.clone();
Collections.sort(v2);
int indexOfMinimumValue =  0;
int indexOfMaximumValue = v2.size() - 1;
CEHJ: You probably mean: int indexOfMinimumValue = v.indexOf(v2.get(0)); // etc.
Yes.

Since you haven't explained yourself very well jtcy, i completely misunderstood your requirement. You should use john's answer
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial