打印字符串数组 [英] Printing String Array

查看:174
本文介绍了打印字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

 的String [] =魔法{大棒,帽子,女巫};
串MAGIC1 = magic.toString();
字符串magic2 = Arrays.toString(魔法);的System.out.println(MAGIC1); //这是印刷的存储器位置
的System.out.println(magic2); //这个打印:[棒,帽子,女巫]

什么是 MAGIC1 magic2 之间的区别?


解决方案

数组是对象,但他们不改变(覆盖)的的toString()方法,该方法意味着它们将使用默认的,由继承的对象。如果你读这种方法的文档,你会发现:


  

Object类的toString方法返回一个包含其中的对象是一个实例,该符号字符'@'的类名称的字符串,散列$ C的无符号十六进制再presentation对象$ C。换句话说,该方法返回字符串等于的值:

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


在你的情况


  • 的getClass()。的getName()收益 [Ljava.lang.String 这意味着

    • 一维数组(因为只有一个 [

    • 它的全名是 java.lang.String中 类型


  • Integer.toHexString(哈希code())返回类似 1db9742 这是十六进制形式整数由散code()返回方法。

如果你看看 Arrays.toString的code

现在(对象[]数组)的String [] 被视为对象[]

 <一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4531\"相对=nofollow> 4531  公开 静态 字符串的toString(<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/Object.java#Object%5B%5D\"相对=nofollow称号=的java.lang.Object []>对象 [] A){
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4532\"相对=nofollow> 4532 如果(一==
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4533\"相对=nofollow> 4533 收益空;
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4535\"相对=nofollow> 4535 INT 的IMAX =则为a.length - 1;
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4536\"相对=nofollow> 4536 如果(IMAX == -1)
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4537\"相对=nofollow> 4537 收益[];
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4539\"相对=nofollow> 4539 <一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/StringBuilder.java#StringBuilder\"相对=nofollow称号=java.lang.StringBuilder中的>的StringBuilder B = 的StringBuilder ();
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4540\"相对=nofollow> 4540 湾<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/StringBuilder.java#StringBuilder.append%28char%29\"相对=nofollow称号=java.lang.StringBuilder.append(字符):StringBuilder的>追加('[');
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4541\"相对=nofollow> 4541 INT 我= 0;我++){
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4542\"相对=nofollow> 4542 湾<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/StringBuilder.java#StringBuilder.append%28java.lang.String%29\"相对=nofollow称号=java.lang.StringBuilder.append(java.lang.String中):StringBuilder的>追加(字符串<一个。 href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/String.java#String.valueOf%28java.lang.Object%29\"相对=nofollow称号=java.lang.String.valueOf(java.lang.Object)中的:字符串>的valueOf (A []​​));
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4543\"相对=nofollow> 4543 如果(我== IMAX)
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4544\"相对=nofollow> 4544 收益乙<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/StringBuilder.java#StringBuilder.append%28char%29\"相对=nofollow称号=java.lang.StringBuilder.append(字符):StringBuilder的>追加(']')<一个。 href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/StringBuilder.java#StringBuilder.toString%28%29\"相对=nofollow称号=java.lang.StringBuilder.toString():字符串> 的toString();
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4545\"相对=nofollow> 4545 湾<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/lang/StringBuilder.java#StringBuilder.append%28java.lang.String%29\"相对=nofollow称号=java.lang.StringBuilder.append(java.lang.String中):StringBuilder的>追加();
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4546\"相对=nofollow> 4546 }
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/8-b132/java/util/Arrays.java#4547\"相对=nofollow> 4547 }

你会看到,它的目的是从该数组的内容创建的字符串版本。它通过遍历所有元素和加入它们的字符串重新presentation到的StringBuilder其然后用于创建字符串将被返回这一点。

Here is my code:

String[] magic = {"stick", "hat", "witch"};
String magic1 = magic.toString();
String magic2 = Arrays.toString(magic);

System.out.println(magic1);         // this is printing a memory location
System.out.println(magic2);         // this one prints: [stick, hat, witch]  

What is the difference between magic1 and magic2?

解决方案

Arrays are objects, but they don't change (override) its toString() method, which means they use default one, inherited from Object. If you read documentation of this method you will find:

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())

In your case

  • getClass().getName() returns [Ljava.lang.String which means
    • one dimensional array (because there is only one [)
    • of type which full name is java.lang.String
  • and Integer.toHexString(hashCode()) returns something like 1db9742 which is hexadecimal form of integer returned by hashCode() method.

Now if you take a look at code of Arrays.toString(Object[] array) (String[] is considered as Object[])

4531  public static String toString(Object[] a) {
4532 if (a == null)
4533 return "null";
4535 int iMax = a.length - 1;
4536 if (iMax == -1)
4537 return "[]";
4539 StringBuilder b = new StringBuilder();
4540 b.append('[');
4541 for (int i = 0; ; i++) {
4542 b.append(String.valueOf(a[i]));
4543 if (i == iMax)
4544 return b.append(']').toString();
4545 b.append(", ");
4546 }
4547 }

you will see that its purpose is to create string build from content of this array. It does this by iterating over all elements and adding their string representation to StringBuilder which is then used to create String which will be returned.

这篇关于打印字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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