Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Vector problem

Posted on 2004-04-17
14
Medium Priority
?
292 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

971 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