• java中,数组toString()方法,数组常用操作


    https://blog.csdn.net/feicongcong/article/details/77893717?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-8.baidujs&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-8.baidujs

    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()的结果也是[类型@哈希值]。

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

    Arrays.toString(a) 

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

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

    char[] data = {'a', 'b', 'c'};
    String str = new String(data);

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

    String.valueOf(char[] ch)


    数组常用操作

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





  • 相关阅读:
    Linux常用命令-学习笔记
    Linux 输入输出重定向
    Linux 网卡配置参数
    JS Promise对象学习
    Linux用户身份与文件权限学习笔记
    4、点击事件
    3、自定义按压效果
    2、自定义背景形状
    1、文字大小,颜色
    5、跑马灯
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14618786.html
Copyright © 2020-2023  润新知