Solved

Vector problem

Posted on 2004-04-17
14
280 Views
Last Modified: 2012-05-04
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~
0
Comment
Question by:jtcy
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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;
      }

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>max = Math.max(max, tempValue);

should have been

max = Math.max(max, tempVal);
0
 

Author Comment

by:jtcy
Comment Utility
Thanks but I need to return the maximum INDEX, not value.
0
 
LVL 4

Expert Comment

by:john-at-7fff
Comment Utility
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
0
 
LVL 4

Expert Comment

by:john-at-7fff
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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;
0
 
LVL 4

Expert Comment

by:john-at-7fff
Comment Utility
He means:

What is the index of the max value?
What is the index of the min value?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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)));
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
And of course if you sort the Vector:

int indexOfMinimumValue =  0;
int indexOfMaximumValue = v.size() - 1;
0
 
LVL 4

Expert Comment

by:john-at-7fff
Comment Utility
jtcy -- one thing you should decide is what the "max index" is when more than one Vector element have the same value.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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;
0
 
LVL 4

Expert Comment

by:john-at-7fff
Comment Utility
CEHJ: You probably mean: int indexOfMinimumValue = v.indexOf(v2.get(0)); // etc.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Yes.

Since you haven't explained yourself very well jtcy, i completely misunderstood your requirement. You should use john's answer
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
   public static int minimum(Vector costs)
       {
          int min = ((Integer) costs.get ( 0 ) ).intValue () ;
          int mini = 0;
          for ( int i = 1, iSize = costs.size () ; i < iSize ; i ++ )
           {
                 Integer temp = ( Integer ) costs.get ( i ) ;
                 int tempVal = temp.intValue () ;

                 if ( tempVal < min )
                 {
                  mini = i ;
                  min = tempVal;
                 }
           }    
          return mini;

     }
     
     public static int maximum(Vector costs)
       {
           int maxi = 0;
          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 )
                 {
                  maxi = i ;
                  max = tempVal;
                 }

           }    
          return maxi;

     }
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now