Testing out code to find the min and max element in an array. My code for finding the maximum works fine .. but my min prints "Minimum element in the array: 0". I probably have one little mistake that is throwing me off lol.
测试代码以查找数组中的min和max元素。我找到最大值的代码很好..但是我的min打印出“数组中的最小元素:0”。我可能有一个小错误让我失去了大声笑。
public void deleteMin() {
// set value for comparision starting from the beginning of the array
int arrayMin = arr[0];
for (int j = 0; j < nElems; j++) {
if (arr[j] < arrayMin) {
arrayMin = arr[j];
}
// insert delete functionality
}
System.out.println("Minimum element in the array: " + arrayMin);
}
3 个解决方案
#1
8
Your code is correct as it stands now. The only reason for arrayMin
to contain 0 after the code has finished is if
您的代码现在正确。代码完成后,arrayMin包含0的唯一原因是if
-
nElems
was not set toarr.length
, or - nElems未设置为arr.length,或
- 0 was indeed the smallest element in the array.
- 0确实是数组中最小的元素。
The code could be simplified.
代码可以简化。
Java 8:
Java 8:
int arrayMin = IntStream.of(arr).min().getAsInt();
Java 7:
Java 7:
int arrayMin = arr[0];
for (int i : arr)
arrayMin = Math.min(arrayMin, i);
(Note that both solutions assume a non-empty array since min would otherwise be undefined.)
(请注意,两个解决方案都假定为非空数组,因为否则将不确定min。)
#2
1
You could do
你可以做到
List<Integer> yourList = new ArrayList<Integer>(arr.length);
for (int i= 0; i< arr.length; i++)
yourList.add(arr[i]);
Collections.min(yourList);
Collections.max(yourList);
See max and min documentation.
请参阅max和min文档。
Or if you have Java 8 as @ioobe already mentionned :
或者,如果你已经提到了@ioobe的Java 8:
IntStream.of(arr).min().getAsInt();
#3
0
If you're just iterating over the array, then utilize the fact that you can have two pointers going instead of just the one.
如果你只是迭代数组,那么利用你可以有两个指针而不仅仅是一个指针的事实。
Hence, this can get the job done with half the number of iterations :
因此,这可以通过一半的迭代次数完成工作:
public static int minElementInArray(int[] a) {
int minArrayElement = Integer.MAX_VALUE;
for (int i = 0; i < a.length / 2; i++) {
if (a[i] < minArrayElement) {
minArrayElement = a[i];
}
if (a[a.length - i - 1] < minArrayElement) {
minArrayElement = a[a.length - i - 1];
}
}
return minArrayElement;
}
#1
8
Your code is correct as it stands now. The only reason for arrayMin
to contain 0 after the code has finished is if
您的代码现在正确。代码完成后,arrayMin包含0的唯一原因是if
-
nElems
was not set toarr.length
, or - nElems未设置为arr.length,或
- 0 was indeed the smallest element in the array.
- 0确实是数组中最小的元素。
The code could be simplified.
代码可以简化。
Java 8:
Java 8:
int arrayMin = IntStream.of(arr).min().getAsInt();
Java 7:
Java 7:
int arrayMin = arr[0];
for (int i : arr)
arrayMin = Math.min(arrayMin, i);
(Note that both solutions assume a non-empty array since min would otherwise be undefined.)
(请注意,两个解决方案都假定为非空数组,因为否则将不确定min。)
#2
1
You could do
你可以做到
List<Integer> yourList = new ArrayList<Integer>(arr.length);
for (int i= 0; i< arr.length; i++)
yourList.add(arr[i]);
Collections.min(yourList);
Collections.max(yourList);
See max and min documentation.
请参阅max和min文档。
Or if you have Java 8 as @ioobe already mentionned :
或者,如果你已经提到了@ioobe的Java 8:
IntStream.of(arr).min().getAsInt();
#3
0
If you're just iterating over the array, then utilize the fact that you can have two pointers going instead of just the one.
如果你只是迭代数组,那么利用你可以有两个指针而不仅仅是一个指针的事实。
Hence, this can get the job done with half the number of iterations :
因此,这可以通过一半的迭代次数完成工作:
public static int minElementInArray(int[] a) {
int minArrayElement = Integer.MAX_VALUE;
for (int i = 0; i < a.length / 2; i++) {
if (a[i] < minArrayElement) {
minArrayElement = a[i];
}
if (a[a.length - i - 1] < minArrayElement) {
minArrayElement = a[a.length - i - 1];
}
}
return minArrayElement;
}