Solved

Vector problem

Posted on 2004-04-17
14
281 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
ID: 10848993
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
ID: 10848997
>>max = Math.max(max, tempValue);

should have been

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

Author Comment

by:jtcy
ID: 10849097
Thanks but I need to return the maximum INDEX, not value.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10849100
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
ID: 10849107
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
ID: 10849108
>>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
ID: 10849111
He means:

What is the index of the max value?
What is the index of the min value?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849115
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
ID: 10849119
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
ID: 10849122
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
ID: 10849125
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
ID: 10849137
CEHJ: You probably mean: int indexOfMinimumValue = v.indexOf(v2.get(0)); // etc.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849147
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
ID: 10851169
   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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
eclipse argument 14 60
tomcat administrtor 12 46
by zero exception 10 39
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

815 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

13 Experts available now in Live!

Get 1:1 Help Now