为什么调用println(阵列)有奇怪的输出? (QUOT; Ljava.lang.String; @ 3e25a5") [英] Why does println(array) have strange output? ("[Ljava.lang.String;@3e25a5")

查看:194
本文介绍了为什么调用println(阵列)有奇怪的输出? (QUOT; Ljava.lang.String; @ 3e25a5")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这四个要素我定义一个字符串数组。为什么当我输入的System.out.println(数组名称),它不输出的元素呢?而是给了我一个奇怪的输出。

下面是我的code ...

 公共类GeniusTrial {    公共静态无效的主要(字串[] args){        的String [] =天才{爱因斯坦,牛顿,哥白尼,开普勒};        System.out.print(天才)
    }
}

下面是我得到的输出:

  [Ljava.lang.String; @ 3e25a5


解决方案

的toString()数组的方法返回一个字符串的阵列,而不是它的内容描述的标识的。这是因为数组不重写<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29\"><$c$c>Object.toString(),其文档解释你所看到的:


  

对于类的toString 方法对象返回一个由类的名字,其中的对象是一串一个实例,该符号字符'@'和对象的哈希code的无符号十六进制再presentation。换句话说,该方法返回字符串等于的值:

 的getClass()。的getName()+'@'+ Integer.toHexString(哈希code())


要获得字符串重数组的内容presentation,你可以使用<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#toString%28java.lang.Object%5B%5D%29\"><$c$c>Arrays.toString(Object[]).

字符串此方法返回由每个元素的的toString()重新presentation,在为了它们出现在数组中,并包含在方括号([])。相邻元素由一个逗号和空格()。

分离

例如,呼唤你的阵列上这一方法将导致以下字符串

 [爱因斯坦,牛顿,哥白尼,开普勒。]

请注意,双逗号和奇数间隔正在造成,因为你的数组元素字符串一切已经有标点和空格在其中。删除这些:

 的String [] =天才{爱因斯坦,牛顿,哥白尼,开普勒};

该方法会再回到这个字符串

 [爱因斯坦,牛顿,哥白尼,开普勒]

重要的是要注意,使用这种方法不会让您欣赏到产生字符串的格式的控制是非常重要的。这是很好的快速检查阵列中的内容,但被限制超出这一目的。例如,如果你不想要的那些封闭方括号,或者要列出每个元素行由行?

在这一点上,你应该开始看到实现自己的方法来输出数组的内容的方式,是专门针对你的任务的价值。正如其他人所建议的,通过实践这个循环和建设新产生的字符串要输出。

您可以找到关于更多信息的这个Java教程文章。一般来说, Java教程是初学者伟大的阅读,应该陪你当然很好。

I have a string array with four elements in it that I defined. How come when I type System.out.println(name of Array), it doesn't output the elements? But instead gives me a weird output.

Here's my code...

public class GeniusTrial {

    public static void main(String[]args) {

        String [] genius = {"Einstein, ", "Newton, ", "Copernicus, ", "Kepler."};

        System.out.print(genius);
    }
}   

Here is the output that I got:

[Ljava.lang.String;@3e25a5

解决方案

The toString() method of an array returns a String describing the identity of the array rather than its contents. That's because arrays don't override Object.toString(), whose documentation explains what you're seeing:

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. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

To get a String representation of an array's contents, you can use Arrays.toString(Object[]).

The String returned by this method consists of each element's toString() representation, in the order they appear in the array and enclosed in square brackets ("[]"). Adjacent elements are separated by a comma and space (", ").

For example, calling this method on your array would result in the following String:

"[Einstein, , Newton, , Copernicus, , Kepler.]"

Note that the double commas and odd spacing are resulting because your array's element Strings already have punctuation and white space in them. Removing those:

String [] genius = {"Einstein", "Newton", "Copernicus", "Kepler"};

The method would then return this String:

"[Einstein, Newton, Copernicus, Kepler]"

It's important to notice that using this method doesn't give you any control over the formatting of the resulting String. It's nice for quickly checking the contents of an array, but is limited beyond that purpose. For example, what if you don't want those enclosing square brackets, or want to list each element line-by-line?

At this point you should start to see the value of implementing your own method to output the contents of your array in a way that's specific to your task. As others have suggested, practice this by using a for loop and building the new resulting String that you want to output.

You can find more information on for loops in this Java Tutorials article. In general, the Java Tutorials are great reading for a beginner and should accompany your course well.

这篇关于为什么调用println(阵列)有奇怪的输出? (QUOT; Ljava.lang.String; @ 3e25a5&QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆