1. int[] arr ={1,2,3,4,5};
  2. String arrString = Arrays.toString(arr);
  3. //输出[I@7150bd4d
  4. System.out.println(arrString);
  5. //输出[1, 2, 3, 4, 5]

java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的。object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toString的方法。

java输出用的函数print();是不接受对象直接输出的,只接受字符串或者数字之类的输出。那么你想把一个创建好的对象拿来输出怎么办

  1. package com.spring.h3;
  2. public class Test2 {
  3. public static void main(String[] args) {
  4. System.out.println("new Test2()==="+new Test2());
  5. //输出结果为:new Test2()===com.spring.h3.Test2@18a992f
  6. }
  7. }

按照print接受的类型来说,s1是不能直接输出的,那么是否代表这个是不能编译运行的呢?当然不是。因为当print检测到输出的是一个对象而不是字符或者数字时,那么它会去调用这个对象类里面的toString 方法,输出结果为[类型@哈希值]。Object类中的toString()方法的源代码如下:

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

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

  所以数组转为字符串应写成:

 

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

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

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

数组常用操作

1. 声明一个数组

  1. String[] arr1 = new String[5];
  2. String[] arr2 = {"a","b","c", "d", "e"};
  3. String[] arr3= new String[]{"a","b","c","d","e"};

2. 输出一个数组

  1. int[] arr = { 1, 2, 3, 4, 5 };
  2. String arrString = Arrays.toString(arr);
  3. // 直接输出,为内存地址
  4. System.out.println(arr);
  5. // [I@139a55
  6. System.out.println(arrString );
  7. // [1, 2, 3, 4, 5]

3. 检查一个数组是否包含某值

  1. String[] arr= { "a", "b", "c", "d", "e" };
  2. boolean b = Arrays.asList(arr).contains("a");
  3. System.out.println(b);
  4. // true

4. 连接两个数组

方法一

  1. //使用Apache Commons Lang library
  2. int[] arr1 = { 1, 2, 3, 4, 5 };
  3. int[] arr2= { 6, 7, 8, 9, 10 };
  4. int[] combArr = ArrayUtils.addAll(arr1 , arr2);

方法二

  1. // System.arraycopy()
  2. static String[] concat(String[] a, String[] b) {
  3. String[] c = new String[a.length + b.length];
  4. System.arraycopy(a, 0, c, 0, a.length);
  5. System.arraycopy(b, 0, c, a.length, b.length);
  6. return c;
  7. }

方法三

  1. //Arrays.copyOf()
  2. public static int[] concat(int[] first, int[] second) {
  3. int[] result = Arrays.copyOf(first, first.length + second.length);
  4. System.arraycopy(second, 0, result, first.length, second.length);
  5. return result;
  6. }

5. 逆向输出一个数组

方法一

  1. // Apache Commons Lang library
  2. int[] arr= { 1, 2, 3, 4, 5 };
  3. ArrayUtils.reverse(intArray);
  4. System.out.println(Arrays.toString(intArray));
  5. //[5, 4, 3, 2, 1]

方法二

  1. int[] arr = { 1, 2, 3, 4, 5 };
  2. int[] revArr = new int[arr.length];
  3. for(int i = 0; i < arr.length; i++){
  4. revArr[i] = arr[arr.length - i -1];
  5. }
  6. System.out.println(Arrays.toString(revArr));
  7. //[5, 4, 3, 2, 1]

6. 移除数组中的元素

    1. // Apache common lang
    2. int[] arr= { 1, 2, 3, 4, 5 };
    3. int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
    4. System.out.println(Arrays.toString(removed))

数组toString()方法,数组常用操作的更多相关文章

  1. day06 字典、元组、set的方法及常用操作

    今日内容: 1.深浅拷贝 2.元组 3.字典 4.set 1.深浅拷贝 # 1.值拷贝 # 采用赋值的方法进行 # 只会将堆区容器变量与栈区的绑定关系进行复制 # 2.浅拷贝 # 会将堆区与栈区的绑定 ...

  2. php数组·的方法-数组与数据结构

    /*数组与数据结构*/ //shuffle() 随机打乱数组 //array_push() 数组末尾添加元素 //array_pop() 数组末尾删除元素 //array_shift() 数组首位删除 ...

  3. linux shell数组赋值方法(常用)

    http://blog.csdn.net/shaobingj126/article/details/7395161 Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... ...

  4. [Android L]SEAndroid开放设备文件结点权限(读或写)方法(涵盖常用操作:sys/xxx、proc/xxx、SystemProperties)

    温馨提示      建议你先了解一下上一篇博文([Android L]SEAndroid增强Androd安全性背景概要及带来的影响)所讲的内容,先对SEAndroid窥个全貌,然后再继续本节内容.   ...

  5. php数组·的方法-数组检索

    /* * //数组检索函数 * */ //array_keys() 获取数组中所有键名 //array_values() 获取数组中所有键名 $arr6=range('a','e'); print_r ...

  6. ECMAScript 6中数组新方法

    数组的方法 数组的的大部分方法都可以实现数组的遍历. foreach方法 实现数组的遍历 const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.forEach(fu ...

  7. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...

  8. JavaScript数组常用操作

    前言 相信大家都用惯了jquery或者underscore等这些类库中常用的数组相关的操作,如$.isArray,_.some,_.find等等方法.这里无非是对原生js的数组操作多了一些包装. 这里 ...

  9. javascript常见操作数组的方法

    在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "obj ...

随机推荐

  1. spring-boot 集成 log4j 记录日志

    1.pom文件中移除和添加依赖 <!-- 移除boot—starter 的log4j --> <dependency> <groupId>org.springfra ...

  2. PHP(Dom操作的一些基础)

    重点!! //DOM操作: // 核心思想:找到元素 操作元素// js找元素 会返回元素对象:// document.getElementById("dd");唯一确定// 返回 ...

  3. /编写一个函数,要求从给定的向量A中删除元素值在x到y之间的所有元素(向量要求各个元素之间不能有间断), 函数原型为int del(int A ,int n , int x , int y),其中n为输入向量的维数,返回值为删除元素后的维数

    /** * @author:(LiberHome) * @date:Created in 2019/2/28 19:39 * @description: * @version:$ */ /* 编写一个 ...

  4. python全栈开发 * 31知识点汇总 * 180716

    31 模块和包一.模块(一)模块的种类:内置模块,自定义模块,扩展模块第三方模块(二)自定义模块 1.模块的创建 : 新建一个py文件. 2.模块名 : 模块名需要符合变量的命名规范. 3.模块的导入 ...

  5. RoR - Introduction to Active Record

    Active Record: ORM ( Object-relational Mapping)Bridges the gap between relational databases , which ...

  6. Java学习-049-正则工具类

    自去年九月份决定再次入学和职业资格进阶,开始备战二者考试至今,以及当下进行中的职称申请,犹如孤独的狼,不断前行在路上,而今凡凡总总的已历8月... 不感慨了,如下为一园友需要的正则工具类,直接上码: ...

  7. soapui调用redis,获取短信验证码

    1.首先,调用redis需要引入redis的jar包,放入到soapui指定目录中,例如我的目录D:\Program Files\SmartBear\SoapUI-Pro-5.1.2\bin\ext ...

  8. Kali2安装完成后的设置

    1.安装中文输入法 vim /etc/apt/sources.list 全部删除,改为国内源 #中科大 deb http://mirrors.ustc.edu.cn/kali kali-rolling ...

  9. IntelliJ IDEA java文件注释模板

    一.设置 二.注释模板 /*** @version: java version 1.7+* @Author : * @Explain :* @contact: * @Time : ${DATE} ${ ...

  10. 只需十四步:从零开始掌握 Python 机器学习(附资源)

    分享一篇来自机器之心的文章.关于机器学习的起步,讲的还是很清楚的.原文链接在:只需十四步:从零开始掌握Python机器学习(附资源) Python 可以说是现在最流行的机器学习语言,而且你也能在网上找 ...