Java 如何在获取元素编号时找到数组中的最小编号
我有个问题一直困扰着我。找到数组中的最小数很容易,但这是我的问题;当您在数组中找到最小的数字时,最小的数字将成为数组的第[0]个元素。 我想要的是,我想要找到最小的数字,以及它是哪个元素。像这样Java 如何在获取元素编号时找到数组中的最小编号,java,arrays,sorting,Java,Arrays,Sorting,我有个问题一直困扰着我。找到数组中的最小数很容易,但这是我的问题;当您在数组中找到最小的数字时,最小的数字将成为数组的第[0]个元素。 我想要的是,我想要找到最小的数字,以及它是哪个元素。像这样 a[0] a[1] a[2] a[3] 5 20 1 12 我希望我的代码写入1是最小的数字,它是数组的[2]元素 我只想从那里得到2,这样我就可以在代码的其余部分使用它了。任何帮助都将不胜感激。谢谢你们 编辑:我以前试过这种方法 int[] dizi = {
a[0] a[1] a[2] a[3]
5 20 1 12
我希望我的代码写入1是最小的数字,它是数组的[2]元素
我只想从那里得到2,这样我就可以在代码的其余部分使用它了。任何帮助都将不胜感激。谢谢你们
编辑:我以前试过这种方法
int[] dizi = {5 , 12, 20, 1};
int gecici;
for (int i=0; i<4; i++) {
for (int y = 1; y<4; y++) {
if (dizi[i] > dizi[y]) {
gecici = dizi[i];
dizi[i] = dizi[y];
dizi[y] = gecici;
}
}
}
int[]dizi={5,12,20,1};
int gecici;
对于(int i=0;i您可以在这种情况下利用IntStream.range
:
IntStream.range(0, arr.length)
.mapToObj(index -> new SimpleEntry<>(index, arr[index]))
.min(Comparator.comparingInt(SimpleEntry::getValue));
IntStream.range(0,arr.length)
.mapToObj(index->newsimplentry(index,arr[index]))
.min(Comparator.comparingit(SimpleEntry::getValue));
例如:
int[] arr = new int[]{5,20 ,1 ,12};
IntStream.range(0, arr.length)
.mapToObj(index -> new SimpleEntry<>(index, arr[index]))
.min(Comparator.comparingInt(SimpleEntry::getValue))
.ifPresent(s -> System.out.println(s.getValue()+
" is the smallest number and it's index" + s.getKey() + "of array"));
int[]arr=newint[]{5,20,1,12};
IntStream.range(0,arr.length)
.mapToObj(index->newsimplentry(index,arr[index]))
.min(Comparator.comparingInt(SimpleEntry::getValue))
.ifPresent(s->System.out.println(s.getValue()+
“是最小的数字,它的索引为”+s.getKey()+“of array”);
int[]arr={3,66,22,44,55};
int small=arr[0];
int指数=0;
对于(inti=0;i试试这个
int[] a = {5, 20, 1, 12};
IntStream.range(0, a.length)
.mapToObj(i -> i)
.min(Comparator.comparing(i -> a[i]))
.ifPresent(i -> System.out.printf(
"%d is the smallest number and it's a[%d] element of array%n", a[i], i));
如果你的数组是双倍的,那么
double[] a = {5, 20, 1, 12};
IntStream.range(0, a.length)
.mapToObj(i -> i)
.min(Comparator.comparing(i -> a[i]))
.ifPresent(i -> System.out.printf(
"%f is the smallest number and it's a[%d] element of array%n", a[i], i));
你可以用一种方法来做
/**
* Returns min index of array x.
* (Returns -1 when length of array x is zero)
*/
static int findMinIndex(int[] x) {
return IntStream.range(0, x.length)
.mapToObj(i -> i)
.min(Comparator.comparing(i -> x[i]))
.orElse(-1);
}
像这样打电话
int[] a = {5, 20, 1, 12};
int minIndex = findMinIndex(a);
System.out.printf("%d is the smallest number and it's a[%d] element of arraay%n",
a[minIndex], minIndex);
int[]数组={0,1,2,3,4,5,6,7,8,9,10};
int smallestNum=数组[0];
int smallestIndex=0;
对于(int i=1;i如果您要查找从小到大的每个元素及其相应的索引,则可以执行以下操作:
public class Test {
public static class Pair implements Comparable<Pair> {
int value;
int index;
public Pair(int _value, int _index) {
this.value = _value;
this.index = _index;
}
@Override
public int compareTo(Pair that) {
return Integer.valueOf(this.value).compareTo(Integer.valueOf(that.value));
}
}
public static void main(String args[]) {
int[] a =new int[]{5, 20, 1, 12};
int n = a.length;
Pair[] p = new Pair[n];
for (int i = 0; i < n; ++i) p[i] = new Pair(a[i], i);
Arrays.sort(p);
for (int i = 0; i < n; ++i) {
System.out.println(i + "th minimum is "+ p[i].value +" and is located at index "+ p[i].index);
}
}
}
您可以发布您尝试过的内容吗?在数组中查找最小的数字很容易,但这是我的问题;当您在数组中查找最小的数字时,最小的数字将成为数组的第[0]个元素。您是否…对数组进行了排序…?排序成本比仅检查最小的元素是谁和在哪里更高。请阅读“如何创建数组”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则,我们无法回答您的问题并为您提供帮助。您的问题到底是什么还不清楚。如果您的代码产生了意外的结果,请将该代码包含在您的问题中。我非常抱歉。我刚刚编辑了关于我尝试的内容的帖子。我对在这里打开帖子非常业余。我希望你们原谅我,为了你们的家庭工作。但请记住Stackoverflow不是用于代码生成的。有些人会避开这样的帖子。快乐的编码!继续学习新的东西,好的。而且,你的例子看起来像一个冒泡排序。难怪数组被重新排序。谢谢。我只是想学习,我甚至不知道为什么我会被否决,但很抱歉,这对数组不起作用{3,0,22,44,55}。并使small=arr[0]
。非常感谢您的回答,但它似乎不起作用int small=arr[0]
尝试更改这一行。它应该可以工作。非常感谢!它确实可以工作!但我想我必须了解InStream。再次感谢您Hello,感谢您的回复。它可以工作于integer,但假设我的数组是double,我该如何工作?非常感谢。我非常感谢您的帮助嘿,呃,我真的很抱歉我的忽略了但是我还有最后一个问题,我怎样才能把i从那里拿出来?因为我会在另一个地方使用它。
int [] array = {0,1,2,3,4,5,6,7,8,9,10};
int smallestNum=array[0];
int smallestIndex=0;
for(int i=1;i<array[i];i++){
if(array[i] < smallestNum){ //if you want the last small number then use `<=` (if small number occur multiple times)
smallestNum = array[i];
smallestIndex=i;
}
}
public class Test {
public static class Pair implements Comparable<Pair> {
int value;
int index;
public Pair(int _value, int _index) {
this.value = _value;
this.index = _index;
}
@Override
public int compareTo(Pair that) {
return Integer.valueOf(this.value).compareTo(Integer.valueOf(that.value));
}
}
public static void main(String args[]) {
int[] a =new int[]{5, 20, 1, 12};
int n = a.length;
Pair[] p = new Pair[n];
for (int i = 0; i < n; ++i) p[i] = new Pair(a[i], i);
Arrays.sort(p);
for (int i = 0; i < n; ++i) {
System.out.println(i + "th minimum is "+ p[i].value +" and is located at index "+ p[i].index);
}
}
}
int[] a =new int[]{5, 20, 1, 12};
int n = a.length;
int minValue = Integer.MAX_VALUE, minIndex = 0;
for (int i = 0; i < n; ++i) {
if (minValue > a[i]) {
minValue = a[i];
minIndex = i;
}
}
System.out.println("Minimum value is : "+ minValue+ " and it is located at index: "+ minIndex);