如何查找最大值和最小值

那么如何找到数字组的最大值和最小值。 例如:数字是

int num[] = {1,2,3,4,5,6,7,8,9,2,2,2,2,2,}; 

接下来是如何找出2出现在arrays中的次数。 这就是我的想法。

 char a = "2" int count = 0; if (num.length = a) { count++; System.out.print (count); } 

如何查找数字组的最大值和最小值。

 int num[] = {1,2,3,4,5,6,7,8,9,2,2,2,2,2,}; getMaxValue(num); getMinValue(num); // getting the maximum value public static int getMaxValue(int[] array){ int maxValue = array[0]; for(int i=1;i < array.length;i++){ if(array[i] > maxValue){ maxValue = array[i]; } } return maxValue; } // getting the miniumum value public static int getMinValue(int[] array){ int minValue = array[0]; for(int i=1;i 

出现时使用:

 int arr = new int[10]; // fill arr with values; for(int i=0; i 

使用带有键的TreeMap作为数字,值是出现次数。然后使用firstEntry()lastEntry()获取最小值和最大值.TreeMap将按升序对整数进行排序。

 TreeMap tuples = new TreeMap(); For occurence of 2 ,simply use Integer tuples.get(2);