【问题标题】:java arrangement of lowest to highest on input values输入值从最低到最高的java排列
【发布时间】:2016-06-16 23:19:22
【问题描述】:

请帮助我获取代码以安排最低到最高的输入值。以下是我的示例代码:

 System.out.print("Enter Number of Process: ");
 int p = Integer.parseInt(input.readLine());

 int [] arrival = new int[p];
 int [] size = new int[p];
 for(int i=0;i<p;i++){
 System.out.print("Arrival Time of Process #"+(i+1)+":");
 arrival[i]=Integer.parseInt(input.readLine());

预期的输出必须是:

输入进程数:4
进程#1的到达时间:100
进程#2的到达时间:110
进程#3的到达时间:102
进程#4的到达时间:101
P1:100
P4:101
P3:103
P2:110

如果你熟悉First Come First Serve Algorithm,那与此有关。

【问题讨论】:

  • 另一方面,您似乎并不熟悉“先编码后询问”政策。
  • 道歉。我现在为此输出尝试一些代码。我只是要求一些指导,因为我认为该输出有很多方法。我只需要一个具体的指南。再次抱歉
  • 略微提高了可读性并添加了额外的标签
  • 您的帖子中有错字。您的输入为 102,但输出为 103。

标签: java sorting


【解决方案1】:

你可以使用方法 Arrays.sort(array[]);

    System.out.print("Enter Number of Process: ");
    int p = Integer.parseIntln(input.readLine());

    int [] arrival = new int[p];
    int [] size = new int[p];
    for(int i=0;i<p;i++) {
    System.out.println("Arrival Time of Process #"+(i+1)+":");
    arrival[i]=Integer.parseInt(input.readLine());
    }

    //You can make a copy of arrival[] and then sort it.
    //So arrival[] won't be sorted.
    size = arrival.clone();
    size = Arrays.sort(size);

    for(int i = 0; i < p; i++) {
    System.out.println("P"+(i+1)+": "+size[i]);
    }

【讨论】:

  • 这个答案可以使用更多细节,或者至少是某种解释?
  • 谢谢先生的帮助:)
  • @DonFirim 请查看上面的示例输出。用户输入值然后排序,它还包括您输入的进程号。我可以为每个容器使用另一个数组吗?
  • @JhayCee 我编辑了一些代码。现在只有 size[] 将包含已排序的元素。
【解决方案2】:

使用Arrays.sort

int[] is = {100, 110, 101, 102};
Arrays.sort(is);
for (int i : is) {
    System.out.println(i);
}

输出:

100
101
102
110

【讨论】:

    【解决方案3】:

    您似乎还想在进行排序时跟踪输入值的原始索引。我推荐你阅读的主题是sorting pairs of (value, index) compared by value。请尝试以下方法:

    import java.io.IOException;
    import java.util.Arrays;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Number of Process: ");
        int p = 0;
    
        try {
            p = Integer.parseInt(br.readLine());
        } catch (NumberFormatException nfe) {
            System.err.println("Invalid Format!");
        }
    
        Pair[] arrival = new Pair[p];
        for (int i = 0; i < p; i++) {
            System.out.print("Arrival Time of Process #" + (i + 1) + ":");
            try {
    
                arrival[i] = new Pair(i + 1, Integer.parseInt(br.readLine()));
            } catch (NumberFormatException nfe) {
                System.err.println("Invalid Format!");
            }
        }
        Arrays.sort(arrival);
        for (int i = 0; i < arrival.length; i++) {
            System.out.println("P" + arrival[i].index + ": " + arrival[i].value);
        }
    }
    
    // a little change was made to the original code
    public static class Pair implements Comparable<Pair> {
    
        public final int index;
        public final int value;
    
        public Pair(int index, int value) {
            this.index = index;
            this.value = value;
        }
    
        @Override
        public int compareTo(Pair other) {
            return Integer.valueOf(this.value).compareTo(other.value);
        }
    }
    

    输出是:

    Enter Number of Process: 4
    Arrival Time of Process #1:100
    Arrival Time of Process #2:110
    Arrival Time of Process #3:103
    Arrival Time of Process #4:101
    P1: 100
    P4: 101
    P3: 103
    P2: 110
    

    【讨论】:

    • 非常感谢。这就是我需要的 :) 对您使用的代码不熟悉,但我研究了这些功能。
    • 如果排序(到达)为什么上面的代码?只有排序的值?不是索引?
    • @JhayCee:新类 Pair 可以解决问题。排序仍然基于值,但原始索引作为字段保存在 Pair 类中。
    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多