数组和 Java 字符串错误:[Ljava.lang.String;@19c42c4b
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19477869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array and Java string error: [Ljava.lang.String;@19c42c4b
提问by user2900130
I've created a program that allows a user to enter in Journal entries (up to 7 days) and then allows a person to call up one of those days after they enter in an entry. Unfortunately, this has left me with some weird string error that I'm not familiar with.
我创建了一个程序,允许用户输入日志条目(最多 7 天),然后允许一个人在输入条目后调用其中的一天。不幸的是,这给我留下了一些我不熟悉的奇怪的字符串错误。
Code as follows:
代码如下:
public class eDiary{
public static void main (String args[]){
int[] days = new int[7];//get our days
days[0] = 1;//start with 1 and not 0
days[1] = 2;
days[2] = 3;
days[3] = 4;
days[4] = 5;
days[5] = 6;
days[6] = 7;
String [] events = new String[7];//events for the days
int i = 0;
//asks for input and counts
for(i=0; i<7; i++){
String event = Console.readString("Tell me the major event of day " + days[i] + "\n");
events[i] = event;
}
int journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");
while (journal_entry != 0) {
System.out.println(events);
journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");
//get r dun!
The input and output:
输入和输出:
Tell me the major event of day 1
one
Tell me the major event of day 2
two
Tell me the major event of day 3
thre
Tell me the major event of day 4
four
Tell me the major event of day 5
five
Tell me the major event of day 6
six
Tell me the major event of day 7
seven
Enter what day you want to hear or Enter 0 to stop
1
[Ljava.lang.String;@10181f5b
Enter what day you want to hear or Enter 0 to stop
0
Howdy y'all!
大家好!
Thanks a lot for the quick responses. One thing it seems to be doing now is when replacing
非常感谢您的快速回复。它现在似乎正在做的一件事是更换时
System.out.println(events);
with
和
System.out.println(events[journal_entry]);
Now gives me input such as this:
现在给我这样的输入:
Tell me the major event of day 1
first day
Tell me the major event of day 2
second day
Tell me the major event of day 3
third day
Tell me the major event of day 4
fourth day
Tell me the major event of day 5
fifth day
Tell me the major event of day 6
sixth day
Tell me the major event of day 7
seventh day
Enter what day you want to hear or Enter 0 to stop
1//the day im asking for
second day//spitting out the next day's entry instead of the first day's entry
Enter what day you want to hear or Enter 0 to stop
0//this is me stopping it
采纳答案by BackSlash
It's not an error.
这不是错误。
System.out.println(events);
In this line you are trying to print the array, butthat statement doesn't print the array contents, it only prints the object class name followed by its hashcode.
在这一行中,您尝试打印数组,但该语句不打印数组内容,它只打印对象类名称,后跟其哈希码。
To print the array content you have to use
要打印您必须使用的数组内容
System.out.println(Arrays.toString(events));
Or, if you want, loop through the array and print its values
或者,如果需要,循环遍历数组并打印其值
回答by Stefano Sanfilippo
There is nothing wrong and that's not an error message.
没有任何问题,这不是错误消息。
Instead, it's the string representation of an array. Consider this line:
相反,它是数组的字符串表示形式。考虑这一行:
System.out.println(events);
You are printing the whole array, so you get that representation -- which happens to be a bit ugly, indeed. You want to print only one element, the one corresponding to the selected day. Use:
你正在打印整个数组,所以你得到了那个表示——这确实有点难看。您只想打印一个元素,即与所选日期对应的元素。用:
System.out.println(events[journal_entry]);
And perform bound checks.
并执行边界检查。
回答by Stephen C
The [Ljava.lang.String;@10181f5b
stuff is what you get when you explicitly or implicitly call Object.toString()
and the target object's class doesn't override toString()
. In this case, the issue is that Java array types do not override toString()
.
的[Ljava.lang.String;@10181f5b
东西是你得到什么,当你显式或隐式调用Object.toString()
和目标对象的类没有覆盖toString()
。在这种情况下,问题在于 Java 数组类型不会覆盖toString()
.
If you want to output an array, use java.util.Arrays.toString(...)
to convert it to a String, then output that.
如果要输出数组,请使用java.util.Arrays.toString(...)
将其转换为字符串,然后输出。
But in this case, you actually need to output a specific entry, not the entire array. The fix is to change
但是在这种情况下,您实际上需要输出特定条目,而不是整个数组。解决方法是改变
System.out.println(events);
to
到
System.out.println(events[journal_entry]);
For the record, the stuff above consists of the classes internal name ("[Ljava.lang.String;") and the object's identity hashcode (in hexadecimal).
作为记录,上面的内容由类内部名称(“[Ljava.lang.String;”)和对象的身份哈希码(十六进制)组成。
This is not a "weird error string".
这不是“奇怪的错误字符串”。
回答by Iwo Kucharski
This is not an error. You want to print the value of variable events. [Ljava.lang.String;@10181f5b
means that events is an array of type java.lang.Stringand 10181f5bis hashcode of this variable. What you want to println is event[i] where i is the number of a day.
这不是错误。您想打印变量events的值。[Ljava.lang.String;@10181f5b
装置的事件是类型的数组java.lang.String中和10181f5b是该变量的散列码。你想要 println 的是 event[i] ,其中 i 是一天的数量。
回答by Maroun
The output you are getting is because:
你得到的输出是因为:
In Java, each objecthas toString()
method, the default is displaying the class name representation, then adding @
and then the hashcode.
在Java中,每个对象都有toString()
方法,默认显示类名表示,然后添加@
,然后是hashcode。
You should use Arrays#toString()
, which is implemented this way:
您应该使用Arrays#toString()
,这是实现这种方式:
3860 public static String toString(int[] a) { {
3861 if (a == null)
3862 return "null";
3863 int iMax = a.length - 1;
3864 if (iMax == -1)
3865 return "[]";
3866
3867 StringBuilder b = new StringBuilder();
3868 b.append('[');
3869 for (int i = 0; ; i++) {
3870 b.append(a[i]);
3871 if (i == iMax)
3872 return b.append(']').toString();
3873 b.append(", ");
3874 }
3875 }
This will help you to better understand arrays.
这将帮助您更好地理解数组。
Of course you can manually loop on the array and print it:
当然,您可以手动循环数组并打印它:
for(String event: events) {
System.out.println(event);
}
回答by Prabhakaran Ramaswamy
In java array's are consider as object. you are printing the event array object that's not what you want.
在java数组中被视为对象。您正在打印不是您想要的事件数组对象。
You need to print name of the day in a week. You need to replace
您需要在一周内打印一天的名称。你需要更换
System.out.println(events);
to
到
System.out.println(events[journal_entry]);
回答by adekorir
It won't print out the answer correctly because you just pointed System.out.println() to events which is supposed to be an array pointer and not the actual variable. You should just replace this line with
它不会正确打印出答案,因为您只是将 System.out.println() 指向应该是数组指针而不是实际变量的事件。你应该用
System.out.println(events[journal_entry]);
For it to make sense. Run it with the conmmand and see if it will run properly.
让它有意义。用命令运行它,看看它是否能正常运行。
回答by user2900130
Thanks for all the responses! I was able to resolve the issue. Here's the code if anyone is curious:
感谢所有的回复!我能够解决这个问题。如果有人好奇,这是代码:
public static void main (String args[]){
int[] days = new int[7];//get our days
days[0] = 1;//start with 1 and not 0
days[1] = 2;
days[2] = 3;
days[3] = 4;
days[4] = 5;
days[5] = 6;
days[6] = 7;
String [] events = new String[7];//events for the days
int i = 0;
//asks for input and counts
for(i=0; i<7; i++){
String event = Console.readString("Tell me the major event of day " + days[i] + "\n");
events[i] = event;
int journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n"); while (journal_entry != 0) { System.out.println("On day " + days[i = 0] + " " + events[journal_entry - 1]); journal_entry = Console.readInt("Enter what day you want to hear or Enter 0 to stop \n");
int journal_entry = Console.readInt("请输入您想听的日期或输入 0 停止\n"); while (journal_entry != 0) { System.out.println("On day " + days[i = 0] + " " + events[journal_entry - 1]); journal_entry = Console.readInt("请输入您想听的日期或输入 0 停止\n");