我在从自定义方法返回数组时遇到问题。它编译得很好,但我回来了:
[Ljava.lang.String;@20cf2c80
Press any key to continue . . .
我使用:
System.out.println(getItem(1));
代码:
public static String[] getItem(int e) {
String[] stats = new String[7];
String name = "Null";
String desc = "None";
String typeOf = "0";
String attackAdd = "0";
String defenseAdd = "0";
String canSell = "true";
String canEat = "false";
String earnedCoins = "0";
if (e == 1) {
name = "Pickaxe";
desc = "Can be used to mine with.";
typeOf = "2";
}
return new String[] { name, desc, typeOf};
}
帮忙? :\
最佳答案
数组对象的 toString() 方法实际上不会遍历并生成数组内容的字符串表示形式,而这正是我认为您想要做的。为此,您需要 Arrays.toString()
.
System.out.println(Arrays.toString(getItem(1)));
符号[Ljava.lang.String
是String
数组的Java代码 - 一般来说,数组的默认字符串表示形式是[L
后跟数组元素的类型。然后你会得到一个分号和数组的内存地址(或某种本地唯一的 ID)。
关于Java 返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272380/