java - 排序并显示数组

标签 java arrays

How many elements do you want in the array?
3
Enter an integer to store in the array
66
enter an integer to store in the array
33
enter an integer to store in the array
99
Here are the sorted values:
[33,66,99]

This should be the result.

This is what I made:

import java.util.Scanner;


public class PracTe3Te {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int [] a;
        int size;
        int value;


        Scanner sc;
        sc = new Scanner(System.in);
        System.out.println("How many elements do you want in the array? ");
        size = sc.nextInt();

        a = new int [size];

        System.out.println("the size of array is " + a.length);



            for (int i = 0; i < a.length; i++) 
            {
                System.out.println("Enter an integer to store in the array");
                value = sc.nextInt();
                a[i] = value;


                 int newNum = value;

                 if (value < a.length - 1) { //otherwise array is already full
                     a[value] = value;
                     newNum = newNum + 1;


                 }

            }
             System.out.print(a);

        }

    }

并得到这个结果:

How many elements do you want in the array? 
3
the size of array is 3
Enter an integer to store in the array
4
Enter an integer to store in the array
3
Enter an integer to store in the array
5
[I@60072ffb

如何打印输入项目的数组列表,而不是 [I@60072ffb

最佳答案

使用Arrays.toString() (不要忘记导入java.util.Arrays):

System.out.print(Arrays.toString(a));

数组不会覆盖 toString() (当您尝试打印对象时调用的方法),因此实现默认为 that of Object它由类名和哈希码组成。

关于java - 排序并显示数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658550/

相关文章:

java - 从构造函数调用重写的方法

Java 在循环中迭代字符串数组并一次返回

java - java中的循环定界符,消息损坏到数组

c - 使用 sizeof 作为数组下标时的警告

python - 如何在 Python 中用间隔索引数组的多个项目

C:指针和数组

java - java中大数的模数

java - droidtext 添加图像不起作用

java - 流对象初始化

java - 如何将 boolean 值存储在数组中并用Java输出?