2013-09-16 38 views
3

這是我的代碼。我需要獲取數組的最小值和最大值,以便我可以獲取範圍,只要輸入數字的最小值爲0,請幫助我。謝謝:)如何獲得數組的最小值,最大值?

final AutoCompleteTextView inputValues = (AutoCompleteTextView) findViewById(R.id.txt_input); 
final TextView txtMinimum = (TextView) findViewById(R.id.txtMinimum); 
final TextView txtMaximum = (TextView) findViewById(R.id.txtMaximum); 
final TextView txtRange = (TextView) findViewById(R.id.txtRange); 

Button btncalculate = (Button)findViewById(R.id.btncalculate); 
btncalculate.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     String []values = (inputValues.getText().toString().split(",")); 
     int[] convertedValues = new int[values.length]; 

     // calculate for the minimum and maximum number 
     int min = 0; 
     int max=0; 

     min = max = convertedValues[0]; 
     for (int i = 0; i < convertedValues.length; ++i) { 
      convertedValues[i] =Integer.parseInt(values[i]); 
      min = Math.min(min, convertedValues[i]); 
      max = Math.max(max, convertedValues[i]); 
     } 
     txtMinimum.setText(Integer.toString(min)); 
     txtMaximum.setText(Integer.toString(max)); 

     // calculate for the range 
     int range=max - min; 
     txtRange.setText(Integer.toString(range)); 

    }}); 
+0

使用Java 8流,可以在一行 –

回答

5
int[] convertedValues = new int[10]; 
int max = convertedValues[0]; 

for (int i = 1; i < convertedValues.length; i++) { 
    if (convertedValues[i] > max) { 
     max = convertedValues[i]; 
    } 
} 

類似地找到用於通過改變較小符號的最小值。

+5

我知道這很愚蠢,但是你可以從i = 1開始:) – smerlung

4
int minIndex = list.indexOf(Collections.min(list)); 

public class MinMaxValue { 

    public static void main(String[] args) { 
     char[] a = {'3', '5', '1', '4', '2'}; 

     List b = Arrays.asList(ArrayUtils.toObject(a)); 

     System.out.println(Collections.min(b)); 
     System.out.println(Collections.max(b)); 
    } 
} 
+0

@dipali做到這一點。 ..好和不錯 – Piyush

5

你可以對數組進行排序,並獲得位置0length-1

Arrays.sort(convertedValues); 

int min = convertedValues[0]; 
int max = convertedValues[convertedValues.length - 1]; 

Arrays#sort(int[])

按指定的升序對指定的整數數組進行排序。

所以排序後,第一個元素是最小值,最後一個是最大值。

+0

排序陣列比掃描複雜度高。對於小型數組,這不是問題,但是如果它們變得很大,那麼只需迭代這些值並進行比較就會更快。 – DRobinson

+0

與DRobinson一致,不應該排序,班輪掃描更便宜。 –

3
1. Sort the array. 
2. Minimum is the First element. 
3. Maximum is the last element of the array. 

只是FYI; Advantages of Sorted Array on computation.

+1

關於此策略的一般注意事項:這意味着界限將爲O(n log n)(請參閱:https://www.cs.cmu.edu/~avrim/451f11/lectures/lect0913.pdf)。如果你想去O(n),你需要避免排序。 –

23

使用Collections與您的代碼使用它,你可以找到最小值和最大值。

以下是該示例代碼:

List<Integer> list = Arrays.asList(100,2,3,4,5,6,7,67,2,32); 

    int min = Collections.min(list); 
    int max = Collections.max(list); 

    System.out.println(min); 
    System.out.println(max); 

輸出:

2 
100 
0

我認爲你只是缺少一個檢查。您應該定義的最小和最大變量爲-1,添加以下檢查。

if (min == -1) { 
    min = convertedValues[i]; 
} 
+0

或者,將它們定義爲'Integer.parseInt(values [0])'。他把它們預置爲'convertedValues [0]',但還沒有存儲任何東西,也沒有解析出值或'values [0]'(如循環中的兩行所示) – DRobinson

0
for(int i = 1; i < convertedValues.length; i++) { 
    if(convertedValues[i]>max) 
     max=convertedValues[i]; 
    if(convertedValues[i]<min) 
     min=convertedValues[i]; 
} 
0
public static int[] getMinMax(int[] array) { 

    int min = Integer.MAX_VALUE; //or -1 
    int max = Integer.MIN_VALUE; //or -1 
    for(int i : array) { //if you need to know the index, use int (i=0;i<array.length;i++) instead 
     if(i < min) min = i; //or if(min == -1 || array[i] < array[min]) min = i; 
     if(i > max) max = i; //or if(max == -1 || array[i] > array[max]) max = i; 
    } 
    return new int[] {min, max}; 
} 

分揀需要至少爲O(n的log(n)),n是陣列中元素的量。如果只是查看數組中的每個元素,則找到最小和最大元素位於O(n)中。對於大數組,這要快得多。

0
 int n = Integer.parseInt(values[0]); 
     convertedValues[i] = n; 
     int min = n; 
     int max = n; 

     for(int i = 1; i < convertedValues.length; ++i) { 
      n = Integer.parseInt(values[i]); 
      convertedValues[i] = n; 
      min = Math.min(min, n); 
      max = Math.max(max, n); 
     } 
0

在你的代碼中刪除以下部分:

(min = max = convertedValues[0];) 

和初始化min以1:

(int min = 1) 
相關問題