在没有Arrays.sort的Java中查找数组列表中的最小整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20585702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find smallest integer value in array list in Java without Arrays.sort
提问by Giovanni
How can I find the smallest value in a int array without changing the array order?
如何在不更改数组顺序的情况下找到 int 数组中的最小值?
code snippet:
代码片段:
int[] tenIntArray = new int [10];
int i, userIn;
Scanner KyBdIn = new Scanner(System.in);
System.out.println("Please enter 10 integer numbers ");
for(i = 0; i < tenIntArray.length; i++){
System.out.println("Please enter integer " + i);
userIn = KyBdIn.nextInt();
tenIntArray[i] = userIn;
}
I am not sure how I can find the smallest array value in the tenIntArray and display the position
我不确定如何在 tenIntArray 中找到最小的数组值并显示位置
For example the array holds - [50, 8, 2, 3, 1, 9, 8, 7 ,54, 10]
例如数组包含 - [50, 8, 2, 3, 1, 9, 8, 7 ,54, 10]
The output should say "The smallest value is 1 at position 5 in array
"
输出应该说“ The smallest value is 1 at position 5 in array
”
采纳答案by Alexis C.
Thisfigure should be helpful :
这个数字应该有帮助:
Then to answer your question, what would you do on paper ?
然后回答你的问题,你会在纸上做什么?
- Create and initialize the min value at
tenIntArray[0]
- Create a variable to hold the index of the min value in the array and initialize it to 0 (because we said in 1. to initialize the min at
tenIntArray[0]
) - Loop through the elements of your array
- If you find an element inferior than the current min, update the minimum value with this element and update the index with the corresponding index of this element
- You're done
- 创建并初始化最小值
tenIntArray[0]
- 创建一个变量来保存数组中最小值的索引并将其初始化为 0(因为我们在 1. 中说过要在 处初始化最小值
tenIntArray[0]
) - 循环遍历数组的元素
- 如果发现低于当前min的元素,用这个元素更新最小值,用这个元素对应的索引更新索引
- 你完成了
Writing the algorithm should be straightforward now.
现在编写算法应该很简单了。
回答by user3101937
the first index of a array is zero. not one.
数组的第一个索引为零。不是一个。
for(i = 0; i < tenIntArray.length; i++)
so correct this. the code that you asked is :
所以纠正这一点。你问的代码是:
int small = Integer.MAX_VALUE;
int i = 0;
int index = 0;
for(int j : tenIntArray){
if(j < small){
small = j;
i++;
index = i;
}
}
System.out.print("The smallest value is"+small+"at position"+ index +"in array");
回答by user3102604
Try this:
尝试这个:
//Let arr be your array of integers
if (arr.length == 0)
return;
int small = arr[0];
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < small) {
small = arr[i];
index = i;
}
}
回答by akhil_mittal
The method I am proposing will find both min
and max
.
我提议的方法将同时找到min
和max
。
public static void main(String[] args) {
findMinMax(new int[] {10,40,50,20,69,37});
}
public static void findMinMax(int[] array) {
if (array == null || array.length < 1)
return;
int min = array[0];
int max = array[0];
for (int i = 1; i <= array.length - 1; i++) {
if (max < array[i]) {
max = array[i];
}
if (min > array[i]) {
min = array[i];
}
}
System.out.println("min: " + min + "\nmax: " + max);
}
Obviously this is not going to one of the most optimized solution but it will work for you. It uses simple comparison to track min
and max
values. Output is:
显然,这不是最优化的解决方案之一,但它对您有用。它使用简单的比较来跟踪min
和max
值。输出是:
min: 10
max: 69
min: 10
max: 69
回答by Dark Army
int[] input = {12,9,33,14,5,4};
int max = 0;
int index = 0;
int indexOne = 0;
int min = input[0];
for(int i = 0;i<input.length;i++)
{
if(max<input[i])
{
max = input[i];
indexOne = i;
}
if(min>input[i])
{
min = input[i];
index = i;
}
}
System.out.println(max);
System.out.println(indexOne);
System.out.println(min);
System.out.println(index);
回答by SAM
Here is the function
这是函数
public int getIndexOfMin(ArrayList<Integer> arr){
int minVal = arr.get(0); // take first as minVal
int indexOfMin = -1; //returns -1 if all elements are equal
for (int i = 0; i < arr.size(); i++) {
//if current is less then minVal
if(arr.get(i) < minVal ){
minVal = arr.get(i); // put it in minVal
indexOfMin = i; // put index of current min
}
}
return indexOfMin;
}
回答by J.R
Using Java 8Streams you can create a Binary operator which compares two integers and returns smallest among them.
使用Java 8Streams,您可以创建一个二元运算符,它比较两个整数并返回其中最小的一个。
Let arr is your array
让 arr 是你的数组
int[] arr = new int[]{54,234,1,45,14,54};
int small = Arrays.stream(arr).reduce((x, y) -> x < y ? x : y).getAsInt();