问题描述:我定义了一个类,类名是Job,当我输出Job.toString()是可以按我重载的toString方法输出的,但是如果输出jobs[]这个数组时,只会输出[Lmodel.Job;@45e228。那么这是为什么呢?怎么输出数组内容呢?

解决方法:使用Arrays.toString(jobs)来输出。,

分析: java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的。object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toString的方法。这个方法是干什么的呢? 
  首先我们得了解,java输出用的函数print();是不接受对象直接输出的,只接受字符串或者数字之类的输出。 
  当print检测到输出的是一个对象而不是字符或者数字时,那么它会去调用这个对象类里面的toString 方法,输出结果为[类型@哈希值]。Object类中的toString()方法的源代码如下:

/**
* Returns a string representation of the object. In general, the
* <code>toString</code> method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The <code>toString</code> method for class <code>Object</code>
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `<code>@</code>', 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:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

而数组类中并没有对此方法重写(override),仅仅是重载(overload)为类的静态方法(参见java.util.Arrays)。所以,数组直接使用toString()的结果也是[类型@哈希值]。所以数组转为字符串应写成:Arrays.toString(a) 。

 这种方法的toString()是带格式的,也就是说输出的是[a, b, c],如果仅仅想输出abc则需用以下两种方法:

方法1:直接在构造String时转换。

char[] data = {'a', 'b', 'c'};    只能是char[]或者byte[],不能是String[],
String str = new String(data);
  • 1
  • 2

方法2:调用String类的方法转换。

String.valueOf(char[] ch)

JAVA数组的toString()方法不能直接输出数组内容?的更多相关文章

  1. Java中的toString()方法

    Java中的toString()方法 目录 Java中的toString()方法 1.    对象的toString方法 2.    基本类型的toString方法 3.    数组的toString ...

  2. 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?

    100怎么变成100.00?

  3. JS对象 数组连接 concat() 方法用于连接两个或多个数组。此方法返回一个新数组,不改变原来的数组。 语法 arrayObject.concat(array1,array2,.arrayN)

    concat() 方法用于连接两个或多个数组.此方法返回一个新数组,不改变原来的数组. 语法 arrayObject.concat(array1,array2,...,arrayN) 参数说明: 注意 ...

  4. java 中重写toString()方法

    toString()方法 一般出现在System.out.println(类名.toString()); toString()是一种自我描述方法 本身返回的是 getClass().getName() ...

  5. java中的toString方法

    对于我这种用惯了C++的人来说,突然见到有人写java程序的时候竟然将整数和String类型的变量使用+连接到一起,感到非常奇怪,追究了下原因. 原来所有的java对象都有toString()方法,而 ...

  6. 源码浅谈(一):java中的 toString()方法

    前言: toString()方法 相信大家都用到过,一般用于以字符串的形式返回对象的相关数据. 最近项目中需要对一个ArrayList<ArrayList<Integer>> ...

  7. 数组的toString方法

    数组继承了object类的toString方法,数据类型将按照旧的格式打印,例如: int[] luckyNumbers = {2,3,5,7,11,13}; String s = "&qu ...

  8. Java中的 toString 方法

    1. Object 类中定义有 public String toString() 方法,其返回值是 String 类型,描述当前对象的有关信息: 2. 在进行 String 与其它类型数据的连接操作时 ...

  9. 关于Java中的toString()方法

    package c07; class ewq{ public String toString() { return "ppppppppp"; } public static voi ...

随机推荐

  1. Python3的基本数据类型

    2.1. Python3中六个标准的基本数据类型: Number(数字) String(字符串) Sets(集合) Tuple(元组) List(列表) Dictionary(字典) 2.2. Pyt ...

  2. SQL复制远程数据库数据到本地-及查询结果少显示一列

    网上找了查询结果怎么少显示一列,因为数据很多列,结果不是视图就是嵌套,太麻烦,这里用临时表做 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB', '19 ...

  3. QT 5.12安装

    QT 5.12为最新的LTS版本,将通过该版本开始QT的学习 1.软件下载 QT5.12下载地址:http://download.qt.io/archive/qt/5.12/ 当前最新版本为5.12. ...

  4. jquery获取年月日时分秒当前时间

    获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日  00:00:00格式的当前时间 function getCurrentDate(date){ var y ...

  5. 自定义django中间件

    自定义中间件 第一步:在根目录创建路径Middle/m1.py(注意如果是python2的话Middle下要有__init__.py文件,不然会报找不到模块错误) m1.py的内容: # -*- co ...

  6. 又一年NOIP后的一波总结

    (昨天正式考完了吧...先写一下现在的感受,出成绩以及后续继续更...) 按照国际惯例,还是先讲一下故事吧. Day(~,0] 大概是跟随者时间的推进,气氛越来越紧张吧. 平时好像大家和往常一样,日常 ...

  7. Django基础之简介(二)

    三板斧 from django.shortcuts import render,HttpResponse, redirect ​ HttpResponse # 返回字符串 urls: urlpatte ...

  8. 1. AtomicInteger 、Unsafe 及 CAS方法的整理

    本文摘自: https://blog.csdn.net/fanrenxiang/article/details/80623884 http://ifeve.com/sun-misc-unsafe/ h ...

  9. Java 遇到的困难

    (1)需求:xml 转 json 依赖的包:commons-beanutils-1.8.3.jarcommons-collections-3.2.1.jarcommons-lang-2.6.jarco ...

  10. web开发中SESSION的本质

    有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制.我们先简单的了解一些http的知识,从而理解该协议的无 ...