2012-12-02 58 views
0

我有一个问题。我试图在数组中显示最大值的索引。我能够获得最大的价值;但是,我不知道显示该最大值的索引。显示索引数组

例如,如果指数[3]具有值5和是在一个数组中的最大元素,这将打印:

值[3] = 5是最大的元件

如何做我这样做?

import java.util.Scanner; 

    public class ArrayExample 
    { 
      public static void main(String[] args) 
     { 
      //Question 1 
      int values[] = new int[5] ; 
      System.out.println("Please enter 5 values for this array: "); 
      Scanner keyboard = new Scanner(System.in); 


       System.out.println("Maximum Value = " + getMaxIndex(values) + getMaxValue(values)); 
     } 

     public static int getMaxValue(int[] values){ 
      int maxValue = values[0]; 
      for(int i=0;i<values.length;i++){ 
       if(values[i] > maxValue){ 
        maxValue = values[i]; 
      } 
      } 
     return maxValue; 
    } 
    public static int getHighestMonth(int[] values) { 
      int highest = 0; 
      for (int i = 0; i < 5; i++) { 
       if (values[i] > values[highest]) { 
        highest = i; 
       } 
      } 
      return highest; 
     } 

} 
+1

为什么不使用maxIndex变量并且并行地更新? –

回答

1

你可以使用AbstractMap.SimpleEntry

public static SimpleEntry<Integer, Integer> getMaxValue(int[] values){ 
    int maxValue = Integer.MIN_VALUE; 
    int maxIndex = -1; 
    for(int i=0;i<values.length;i++){ 
     if(values[i] >= maxValue){ 
      maxValue = values[i]; 
      maxIndex = i; 
     } 
    } 
    return new SimpleEntry<Integer, Integer>(maxIndex, maxValue); 
} 

public static void main(String[] args) { 
    SimpleEntry<Integer, Integer> maxEntry = getMaxValue(new int[]{1,2,3}); 

    System.out.println("value["+ maxEntry.getKey() + "] = " + maxEntry.getValue() + " is the largest element"); 
    //value[2] = 3 is the largest element 

} 
+0

不要过度执行任务......它是一项简单的任务 – firephil

+0

@firephil在java中没有办法在不使用对象的情况下返回多个值,不如使用现有类而不是创建新对象。 – Esailija

+0

返回maxIndex是最简单和最好的解决方案,无需复杂化 – firephil

1

希望下面的代码可以帮助你

public static int getMaxIndex(int[] values) { 
    int maxValue = values[0]; 
    int maxIndex = 0; 
    for (int i = 0; i < values.length; i++) { 
     if (values[i] > maxValue) { 
      maxValue = values[i]; 
      maxIndex = i; 
     } 
    } 
    return maxIndex; 
} 
+0

也更新maxValue,否则它是没用的 – hoaz

+0

而我宁愿从'maxValue'中的'Integer.MIN_VALUE'和'maxIndex'中的'-1'开始。 –

+0

感谢这有助于! – etnemo

1

您创建一个实例变量叫做位置:

public class ArrayExample 
    { 
    private static int position = 0; 
    ... 

    } 
在getMaxValue

还保存的位置与包括maxValue一起:

public static int getMaxValue(int[] values){ 
     int maxValue = values[0]; 
     for(int i=0;i<values.length;i++){ 
      if(values[i] > maxValue){ 
       maxValue = values[i]; 
       position = i; 
     } 
     } 
    return maxValue; 
} 

那么你外面打印:

System.out.println("Maximum Value = " + getMaxIndex(values) + "in position" + position); 

或者另一种方法是,返回的位置,而不是包括maxValue:

public static int getMaxValue(int[] values){ 
      int maxValue = values[0]; 
      int position = 0; 
      for(int i=0;i<values.length;i++){ 
       if(values[i] > maxValue){ 
        maxValue = values[i]; 
        position = i; 
      } 
      } 
     return position; 
    } 

和外部的你:

int position = getMaxIndex(values); 
System.out.println("Maximum Value = " + values[position] + "in position" + position); 
+0

这并不是多线程环境中的首选方式。 –

+0

@BhavikAmbani Op提到多线程的地方在哪里? – dreamcrash

+0

你还必须照顾那也 –