我想知道为什么以及如何修复此数组方法输出 temp
的内存表示形式。
public class StringMethodsExersice{
public static int[] shiftMax(int[] num){
int temp[] = new int[num.length];
int firstEl = num[0];
int lastEl = num[num.length-1];
for(int i=1;i<num.length-1;i++){
if(num[i] > num[i+1]){
temp[i] = num[i];
}
temp[i] = num[i];
}
temp[0] = firstEl;
temp[num.length-1]= lastEl;
return temp;
}
public static void main (String []args){
int[] myArray = new int [5];
myArray [0] = 2;
myArray [1] = 5;
myArray [2] = 6;
myArray [3] = 14;
myArray [4] = 25;
System.out.println(shiftMax(myArray));
}
}
最佳答案
您可以使用Arrays#toString
:
System.out.println(Arrays.toString(shiftMax(myArray)));
(详情请参阅 its implementation)
<小时/>有关您获得的输出的说明:
由于每个对象都有 toString()
方法,因此默认显示类名表示形式,然后添加 @
符号,然后添加哈希码。
关于java - java中奇怪的数组输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283005/