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/