![](/img/trans.png)
[英]I can't find smallest number by this code. How I find smallest number by modifying this code?
[英]How can i find the smallest number in array while getting the element number
我有一個困擾我的問題。 在數組中找到最小的數字很容易,但這是我的問題。 當您在數組中找到最小的數字時,最小的數字將成為數組的第[0]個元素。 我想要的是,我想找到最小的數字,以及它是哪個元素。 這樣的
a[0] a[1] a[2] a[3]
5 20 1 12
我希望我的代碼寫1 is the smallest number and it's a[2] element of array
我只想從那里得到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;
}
}
}
在這種情況下,您可以利用IntStream.range
:
IntStream.range(0, arr.length)
.mapToObj(index -> new SimpleEntry<>(index, arr[index]))
.min(Comparator.comparingInt(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 = {3,66,22,44,55};
int small=arr[0];
int index=0;
for(int i=0;i<arr.length;i++){
if(arr[i]<small){
small = arr[i];
index = 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,則
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 [] 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);
}
}
}
上述方法的復雜度將是時間復雜度O(n log n)
。 但是,如果您只需要知道最小的索引及其索引,則可以按O(n)
時間復雜度輕松地檢索它,如下所示:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.