[英]Returning an Array from a method (doesn't seem to be working)
import java.util.*; 导入java.util。*;
public class test2 { 公共课程test2 {
public static void main(String[] args){
int[] list = {1,2,3,4,5,6,7};
int[] list2 = reverse(list);
System.out.print(list2);
}
public static int[] reverse(int[] list){
int[] result = new int[list.length];
for(int i=0, j=list.length-1; i<list.length; i++,j--){
result[j] = list[i];}
return result; }
}
it is a code from my textbook. 这是我教科书上的代码。 But when I print list2, it shows this weird message : [I@7b963273 I have no idea what is wrong with the code.
但是当我打印list2时,它显示了这个奇怪的消息:[I @ 7b963273我不知道代码有什么问题。 Plz help me!
请帮我!
The array of primitive types is an object itself . 基本类型数组是一个对象本身。 When you print it using System.out.print() , it calls toString() method of Object class.
使用System.out.print()打印时,它会调用Object类的toString()方法。 Which is defined like this in Object class:
在Object类中定义如下:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
to print the returned array you use: 打印您使用的返回数组:
System.out.println(Arrays.toString(list2));
EDIT 编辑
If you want to return an array from method you should do exactly how you did in your code. 如果要从方法返回数组,则应完全按照您在代码中所做的操作。
suppose that 'merge' is a method and arr1 and arr2 are arrays then you can do like this:
假设'merge'是一个方法,而arr1和arr2是数组,那么您可以这样做:
public static int[] merge(int arr1[], int arr2[])
{
int[] result = new int[arr1.length + arr2.length];
int counter = 0 ;
for ( int i =0 ;i < result.length ; i++)
{
if ( i >= arr1.length)
{
result[i] = arr2[counter++];
}
else
{
result[i] = arr1[i];
}
}
return result;
}
And then you can print the merge as: 然后您可以将合并打印为:
System.out.println(Arrays.toString(merge(arr1,arr2));
You're calling toString()
(implicitly) directly on an array. 您正在直接在数组上调用
toString()
(隐式)。 Arrays don't override toString()
, so you get the default implementation from Object
: 数组不会覆盖
toString()
,因此您可以从Object
获取默认实现 :
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.
Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号字符“ @”以及该对象的哈希码的无符号十六进制表示形式。 In other words, this method returns a string equal to the value of:
换句话说,此方法返回的字符串等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
So in your output, the [I
part is the "name" of the int[]
class (print int[].class.getName()
and it will show the same thing) and the number is the hash code of the array in hex. 因此,在您的输出中,
[I
部分是int[]
类的“名称”(打印int[].class.getName()
并显示相同的内容),数字是数组中的哈希码十六进制。 (Which again isn't overridden - it doesn't take account of the contents of tha array.) (这又不会被覆盖-它没有考虑tha数组的内容。)
This has nothing to do with where the array has come from - you'd get the same kind of output (with a different number) if you called: 这与数组的来源无关-如果您调用以下命令,则会得到相同类型的输出(具有不同的数字):
System.out.print(list);
before the reverse
call. 在
reverse
呼叫之前。
You should use Arrays.toString(int\\[\\])
: 您应该使用
Arrays.toString(int\\[\\])
:
System.out.println(Arrays.toString(list2));
System.out.println(list2);
invokes Object#toString()
method which returns the combination of Classname+@+Hex_Number_Of_Hashcode
. 调用
Object#toString()
方法,该方法返回Classname+@+Hex_Number_Of_Hashcode
的组合。
you have to use overloaded Arrays.toString(list2)
; 您必须使用重载的
Arrays.toString(list2)
; to print the string representation of your array. 打印数组的字符串表示形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.