8

Here's my code. I need to get the minimum,maximum value of my array to be able for me get the range, whenever I input numbers the minimum value is 0. Please help me. Thank you:)

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));

    }});
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Dio
  • 117
  • 1
  • 2
  • 6

10 Answers10

29

Use Collections with your code using it you can find minimum and maximum .

following is the example code for that:

 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);

Output:

2
100
Simmant
  • 1,477
  • 25
  • 39
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
7
int[] convertedValues = new int[10];
int max = convertedValues[0];

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

Similarly find for the minimum value by changing lesser symbol.

Balaji Dhanasekar
  • 1,066
  • 8
  • 18
6
int minIndex = list.indexOf(Collections.min(list));

or

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));
   }
}
dipali
  • 10,966
  • 5
  • 25
  • 51
5

You could sort the array and get the positions 0 and length-1:

Arrays.sort(convertedValues);

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

Arrays#sort(int[]):

Sorts the specified array of ints into ascending numerical order.

So, after sorting, the first element is the minimum, the last is the maximum.

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 1
    Sorting the array is of higher complexity than scanning. For small arrays this isn't an issue, but if they get quite large then simply iterating over the values and comparing will certainly be faster. – DRobinson Sep 16 '13 at 12:44
  • agreed with DRobinson, should not go sort, liner scan is cheaper. – Russel Yang Nov 28 '17 at 19:31
3
1. Sort the array.
2. Minimum is the First element.
3. Maximum is the last element of the array.

Just FYI; Advantages of Sorted Array on computation.

Community
  • 1
  • 1
JNL
  • 4,683
  • 18
  • 29
  • 1
    a general note on this strategy: it means the bound will be O(n log n) (see: https://www.cs.cmu.edu/~avrim/451f11/lectures/lect0913.pdf). in cases where you want to get to O(n) you'll need to avoid sorting. – Alex Moore-Niemi Jan 16 '17 at 02:20
1
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};
}

Sorting takes at least O(n log(n)), n being the amount of elements in the array. If you simply look at every element in the array, finding the minimum and maximum element is in O(n). For big arrays, that's a lot faster.

brickBreaker
  • 339
  • 2
  • 10
0

I think you are just missing one check .. You should define the min and max variables as -1 and add following check.

if (min == -1) {
    min = convertedValues[i];
}
user1401472
  • 2,203
  • 3
  • 23
  • 37
  • Or, instead, define them as `Integer.parseInt(values[0])`. He has them preset as `convertedValues[0]` but hasn't yet stored anything in it, nor parsed the value out or `values[0]` (as is done two lines lower in the loop) – DRobinson Sep 16 '13 at 12:39
0
for(int i = 1; i < convertedValues.length; i++) {
    if(convertedValues[i]>max) 
        max=convertedValues[i];
    if(convertedValues[i]<min)
        min=convertedValues[i]; 
}
gifpif
  • 4,507
  • 4
  • 31
  • 45
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);
        }
Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
0

In your code remove the following part:

(min = max = convertedValues[0];)

and initialize min to 1:

(int min = 1) 
stkent
  • 19,772
  • 14
  • 85
  • 111