0

質問があります。最大値のインデックスを配列で表示しようとしています。最大値を取得できました。ただし、その最大値のインデックスを表示する方法がわかりません。

たとえば、インデックス [3] の値が 5 で、配列内の最大の要素である場合、次のように出力されます。

value[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;
        }

}
4

3 に答える 3

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;
}
于 2012-12-02T17:09:23.783 に答える
1

position というインスタンス変数を作成します。

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);
于 2012-12-02T17:10:28.293 に答える
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

}
于 2012-12-02T17:12:23.957 に答える