简体   繁体   English

用Java打印排序数组

[英]Printing sorted array in Java

What the code does is, basically, asks for number of elements that you want to sort,the elements and prints them sorted. 从根本上讲,代码的作用是询问您要排序的元素数量,然后将这些元素打印出来进行排序。 Or at least it should. 或者至少应该如此。 When I run it it would do that: 当我运行它会做到这一点:

在此处输入图片说明

What should I do? 我该怎么办? I'm using JDK 7 and IntelliJ IDEA 15 . 我正在使用JDK 7IntelliJ IDEA 15

And yes, I did google it and I couldn't find anything. 是的,我用Google做了它,却找不到任何东西。 And no , I don't want the code, I want opinions. 不,我不需要代码,我想要意见。

import java.text.MessageFormat;
import java.util.Scanner;

public class bb {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number of elements:");
        int numberOfElements = scan.nextInt();
        System.out.print(MessageFormat.format("Enter {0} {1} ",numberOfElements,"numbers: "));
        int [] elements = new int[numberOfElements];
        for (int i = 0; i < elements.length; i++) {
            elements[i]=scan.nextInt();
            java.util.Arrays.sort(elements);
        }
        System.out.println(java.util.Arrays.toString(elements));
    }
}

Doing sorting in loop will consider the default value (0) in elements as another value. 循环进行排序会将elements的默认值(0)视为另一个值。 Thus after sorting, the minimum value in array will be 0 which should not be. 因此,排序后,数组中的最小值将为0,不应为0。

Solution, do sorting outside loop. 解决方案,做外循环sorting

for (int i = 0; i < elements.length; i++) {
    elements[i]=scan.nextInt();
}
java.util.Arrays.sort(elements);

You're sorting your list everytime the loop happens which is unnecessary, instead you should use: 每次循环时都在对列表进行排序,这是不必要的,而应使用:

for (int i = 0; i < elements.length; i++) {
    elements[i]=scan.nextInt();
}
java.util.Arrays.sort(elements);

By running java.util.Arrays.sort(elements); 通过运行java.util.Arrays.sort(elements); everytime in the loop, you're essentially saying that you want to sort the elements everytime a number is entered and this is what happens: 每次循环时,您实际上是在说要在每次输入数字时对elements进行排序,这是发生的情况:

Enter number of elements:2
Enter 2 numbers:  3
sort being called on[0, 3] 
1
sort being called on[0, 1]
[0, 1]

The reason for the 0 is that that when you initialise an int array with n number of elements you have this: 0的原因是,当您初始化一个具有n个元素的int数组时,您将具有以下内容:

int[] x = new int[3]; // this is making [0,0,0]

So when you try and sort this after adding one element 2 -> Arrays.sort([2,0,0]) you will get [0,0,2] and hence it will cause issues. 因此,当您在添加一个元素2 > Arrays.sort([2,0,0])之后尝试Arrays.sort([2,0,0])您将得到[0,0,2] ,因此将引起问题。

Issue i see is You are sorting array even when your still getting input from user. 
e.g.
You initialized array to size 3 [0,0,0]
in first loop user provides 4  [4,0,0] after sort [0,0,4]
in Second loop user provides 7 [0,7,4] after sort [0,4,7]
in third loop user provides 1 [0,4,1]  after sort [0,1,4]
here your value is getting overwritten.  

write sort function after you finish getting input from user. 

There are two things that need to be noted to understand why your code is not working. 要了解为什么您的代码不起作用,需要注意两点。

  1. Arrays are initialized with default values on creation. 数组在创建时使用默认值初始化。
  2. The call to sort is placed inside the for loop which does a sort at every step. 对sort的调用放置在for循环中,该循环在每一步进行排序。

Explanation: 说明:

Whenever you provide "number of elements" = 2, the element is read for position elements[0] and on call to sort statement, it gets placed at position elements[1]. 每当您提供“元素数” = 2时,就会读取位置element [0]的元素,并在调用sort语句时将其放置在position elements [1]处。 Then, in the next iteration, the element is read and placed at position elements[1], overwriting the value at the position. 然后,在下一次迭代中,将读取元素并将其放置在位置elements [1]处,覆盖该位置的值。 Hence, you always land up with a zero and second input number with number of elements =2. 因此,您总是以零个和第二个输入数字着陆,元素数量= 2。

A similar case will happen with more than two elements, where one or more elements may get overwritten. 如果两个以上的元素发生类似的情况,则一个或多个元素可能会被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM