Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回未排序列表第n个最大值的索引_Java_Arrays_List_Search_Indexing - Fatal编程技术网

Java 返回未排序列表第n个最大值的索引

Java 返回未排序列表第n个最大值的索引,java,arrays,list,search,indexing,Java,Arrays,List,Search,Indexing,我已经编写了以下代码,现在正试图找出实现四条注释中所述内容的最佳方法: Integer[] expectedValues = new Integer[4]; for (int i = 0; i <= 3; i++) { expectedValues[i] = getExpectedValue(i); } int choice = randomNumGenerator.nextInt(100) + 1; if (choice <

我已经编写了以下代码,现在正试图找出实现四条注释中所述内容的最佳方法:

    Integer[] expectedValues = new Integer[4];

    for (int i = 0; i <= 3; i++) {
        expectedValues[i] = getExpectedValue(i);
    }

    int choice = randomNumGenerator.nextInt(100) + 1;
    if (choice <= intelligence) {
        // return index of highest value in expectedValues
    } else if (choice <= intelligence * 2) {
        // return index of 2nd highest value in expectedValues
    } else if (choice <= intelligence * 3) {
        // return index of 3rd highest value in expectedValues
    } else {
        // return index of lowest value in expectedValues
    }
Integer[]expectedValues=新整数[4];

对于(int i=0;i您可以创建一个包含索引的新数组,并对值进行排序-在半伪代码中,它可以如下所示(待调整):


如果您想使用简单阵列,这可能是一个解决方案:

public static void main(String[] args) {
    int[] arr = new int[] { 1, 4, 2, 3 };
    int[] sorted = sortedCopy(arr);

    int choice = randomNumGenerator.nextInt(100) + 1;
    if (choice <= intelligence) {
        System.out.println(findIndex(arr, sorted[3])); // 1
    } else if (choice <= intelligence * 2) {
        System.out.println(findIndex(arr, sorted[2])); // 3
    } else if (choice <= intelligence * 3) {
        System.out.println(findIndex(arr, sorted[1])); // 2
    } else {
        System.out.println(findIndex(arr, sorted[0])); // 0
    }
}

static int[] sortedCopy(int[] arr) {
    int[] copy = new int[arr.length];
    System.arraycopy(arr, 0, copy, 0, arr.length);
    Arrays.sort(copy);
    return copy;
}

static int findIndex(int[] arr, int val) {
    int index = -1;
    for (int i = 0; i < arr.length; ++i) {
        if (arr[i] == val) {
            index = i;
            break;
        }
    }
    return index;
}
publicstaticvoidmain(字符串[]args){
int[]arr=新的int[]{1,4,2,3};
int[]已排序=已排序副本(arr);
int choice=randomNumGenerator.nextInt(100)+1;
如果(选择您可以“擦除”最高值n-1次。在此之后,最高值是原始数组的第n个最高值:

public static void main(String[] args) {
    int[] numbers = new int[]{5, 9, 1, 4};
    int n = 2; // n-th index

    for (int i = 0; i < n - 1; ++i) {
        int maxIndex = findMaxIndex(numbers);
        numbers[maxIndex] = Integer.MIN_VALUE;
    }

    int maxIndex = findMaxIndex(numbers);
    System.out.println(maxIndex + " -> " + numbers[maxIndex]);
}

public static int findMaxIndex(int[] numbers) {
    int maxIndex = 0;
    for (int j = 1; j < numbers.length; ++j) {
        if (numbers[j] > numbers[maxIndex]) {
            maxIndex = j;
        }
    }
    return maxIndex;
}
publicstaticvoidmain(字符串[]args){
int[]数字=新的int[]{5,9,1,4};
int n=2;//第n个索引
对于(int i=0;i”+数字[maxIndex]);
}
公共静态int findMaxIndex(int[]数字){
int maxIndex=0;
对于(int j=1;j数字[maxIndex]){
maxIndex=j;
}
}
返回最大索引;
}

复杂性是O(n*numbers.length)

@assylias在这种情况下,数组的索引不仅仅是索引。
getExpectedValues(i)
正在获取
0
1
2
3
所需的特定值,然后我需要返回数组第n个最高值的索引,而不是返回值本身。@nvioli
getExpectedValue()
只能将
0
1
2
3
作为参数,因此循环仅用于计算这4个值。例如,如果函数只能将
6
7
8
9
作为参数,则我的循环将是
(int i=6;i这是可行的,但有点混乱。也许
映射会更干净?@lucasvw我最初的想法是使用映射,但我最终必须返回使用值检索到的a键-映射的工作方式与此相反,因此这也会变得非常复杂messy@lucasvw使用地图时,您需要按键排序-因此数组中的索引将是映射中的值,数组中的值将是键,如果存在相等的值,则可能会导致冲突。
int[]copy=new int[arr.length];System.arraycopy(arr,0,copy,0,arr.length);
可以更简单地编写:
int[]copy=arr.clone();
public static void main(String[] args) {
    int[] numbers = new int[]{5, 9, 1, 4};
    int n = 2; // n-th index

    for (int i = 0; i < n - 1; ++i) {
        int maxIndex = findMaxIndex(numbers);
        numbers[maxIndex] = Integer.MIN_VALUE;
    }

    int maxIndex = findMaxIndex(numbers);
    System.out.println(maxIndex + " -> " + numbers[maxIndex]);
}

public static int findMaxIndex(int[] numbers) {
    int maxIndex = 0;
    for (int j = 1; j < numbers.length; ++j) {
        if (numbers[j] > numbers[maxIndex]) {
            maxIndex = j;
        }
    }
    return maxIndex;
}