如何从未排序的数组中获取 m 个最小元素的索引,而不更改实际数组? [英] How to get index of m smallest elements from an unsorted array, without changing the actual array?

查看:52
本文介绍了如何从未排序的数组中获取 m 个最小元素的索引,而不更改实际数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个临时数组来存储用户的输入并对其进行排序以获取 m 个最小元素并引用 originalArray 的原始索引,然后打印 m 个最小元素的索引但是当我运行我的代码,我得到的只是-1.我的元素不应超出原始数组的范围,因为它取自 originalArray.为什么我得到 -1?

I wanted to create a temporary array to store the input by the user and sort it to get the m smallest elements and refer to the original index of the originalArray and then print the index of the m smallest elements but when I run my code, all I get is -1. My elements should not be out of bounds of the original array as it is taken from originalArray. Why am I getting -1?

import java.util.*;
public class MinimumSelection
{
    public static void main(String[] args)
    {
        //Array and variable declarations
        int[] originalArray;
        int[] tempArray;
        int tempValue;
        int lowestValue;
        int arrayLength;
        String elementValue;

        //Prompt the user for random numbers as an array input
        System.out.println("Please Enter your array length");
        arrayLength = Integer.parseInt(new Scanner(System.in).nextLine());
        //Input feeds as the length of the array as entered by user
        originalArray = new int[arrayLength];
        //Storing the input value in temporary array which will be used to sort
        tempArray = new int[arrayLength];

        //prompt user to enter elements of the orignial array
        for (int element = 0; element < originalArray.length; element++)
        {
            System.out.printf("\n Enter array elements %1$s: " + "\r\n", element + 1);
            elementValue = new Scanner(System.in).nextLine();
            originalArray[element] = Integer.parseInt(elementValue);
        }

        System.arraycopy(originalArray, 0, tempArray, 0, originalArray.length);

        //Sorting the original array
        for (int write = 0; write < tempArray.length; write++)
        {
            for (int sort = 0; sort < tempArray.length - 1 - write; sort++)
            {
                if (tempArray[sort] > tempArray[sort + 1])
                {
                    tempValue = tempArray[sort + 1];
                    tempArray[sort + 1] = tempArray[sort];
                    tempArray[sort] = tempValue;
                }
            }
        }

        //promoting user to enter no. of smallest elements they want this program to display
        System.out.println("Please Enter number of smallest element");
        lowestValue = Integer.parseInt(new Scanner(System.in).nextLine());

        //display output
        System.out.println("Result :");
        for (int loop = 0; loop < lowestValue; loop++)
        {
            int x = tempArray[loop];
            int y = Arrays.asList(originalArray).indexOf(x);
            // Arrays.asList(array).indexOf(4);

            System.out.println((new Integer(y)).toString());
        }
        new Scanner(System.in).nextLine();
    }
}

推荐答案

创建一个 TreeMap,其中键是元素,值是它在原始数组中的索引.然后你遍历这个映射的条目并在前 m 步后跳出循环.

Create a TreeMap where the key is the element and the value is its index in the original array. Then you iterate through the entries of this map and break out of the loop after the first m steps.

这篇关于如何从未排序的数组中获取 m 个最小元素的索引,而不更改实际数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆