Arrays.sort(array); Scanner input = new Scanner(System.in); while(input.hasNextInt(array.length)) { int n = input.nextInt(array.length); System.out.println("The number is :"+array[n]); System.out.println("The index of the number above is : "+sorted.get(array[n])); } } }non-API algorithm coming soon. :)
恩,我先看看,如果您有空,能帮忙来个不用API 的旧态感谢拉。 我先学习学习你的
public class PaiArr{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); int[] a = new int[]{9, 8, 6, 7, 3, 1, 5, 10}; int[] b = new int[a.length]; //赋值 for(int i=0; i<a.length; i++){ b[i] = a[i]; }
- -
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;public class ArraySample { public static void main(String[] args) {
int[] array = {3,45,6,37,78,35,28,59,39,58,24};
Map<Integer, Integer> sorted = new TreeMap<Integer, Integer>();
for(int i=0;i<array.length;i++)
sorted.put(array[i], i);
Arrays.sort(array);
Scanner input = new Scanner(System.in);
while(input.hasNextInt(array.length)) {
int n = input.nextInt(array.length);
System.out.println("The number is :"+array[n]);
System.out.println("The index of the number above is : "+sorted.get(array[n]));
}
}
}non-API algorithm coming soon. :)
我先学习学习你的
public class PaiArr{
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int[] a = new int[]{9, 8, 6, 7, 3, 1, 5, 10};
int[] b = new int[a.length];
//赋值
for(int i=0; i<a.length; i++){
b[i] = a[i];
}
//对a排序
for(int i=0; i<a.length; i++) {
for(int j=0; j<a.length-i-1; j++) {
if(a[j] > a[j+1]) {
swap(a, j, j+1);
}
}
}
//求值
System.out.println("n最大值:"+a[n]);
for(int m=0;m<b.length;m++){
System.out.println(b[m]);
if(b[m]==a[n]){
System.out.println("值所在位置:"+m);
}
}
}
public static void swap(int a[] , int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}