Solved

Vector problem

Posted on 2004-04-17
14
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

630 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