数组toString()方法,数组常用操作
- int[] arr ={1,2,3,4,5};
- String arrString = Arrays.toString(arr);
- //输出[I@7150bd4d
- System.out.println(arrString);
- //输出[1, 2, 3, 4, 5]
java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的。object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toString的方法。
java输出用的函数print();是不接受对象直接输出的,只接受字符串或者数字之类的输出。那么你想把一个创建好的对象拿来输出怎么办
- package com.spring.h3;
- public class Test2 {
- public static void main(String[] args) {
- System.out.println("new Test2()==="+new Test2());
- //输出结果为:new Test2()===com.spring.h3.Test2@18a992f
- }
- }
按照print接受的类型来说,s1是不能直接输出的,那么是否代表这个是不能编译运行的呢?当然不是。因为当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());
- }
而数组类中并没有对此方法重写(override),仅仅是重载(overload)为类的静态方法(参见java.util.Arrays)。所以,数组直接使用toString()的结果也是[类型@哈希值]。
所以数组转为字符串应写成:
这种方法的toString()是带格式的,也就是说输出的是[a, b, c],如果仅仅想输出abc则需用以下两种方法:
方法1:直接在构造String时转换。
方法2:调用String类的方法转换。
数组常用操作
1. 声明一个数组
- String[] arr1 = new String[5];
- String[] arr2 = {"a","b","c", "d", "e"};
- String[] arr3= new String[]{"a","b","c","d","e"};
2. 输出一个数组
- int[] arr = { 1, 2, 3, 4, 5 };
- String arrString = Arrays.toString(arr);
- // 直接输出,为内存地址
- System.out.println(arr);
- // [I@139a55
- System.out.println(arrString );
- // [1, 2, 3, 4, 5]
3. 检查一个数组是否包含某值
- String[] arr= { "a", "b", "c", "d", "e" };
- boolean b = Arrays.asList(arr).contains("a");
- System.out.println(b);
- // true
4. 连接两个数组
方法一
- //使用Apache Commons Lang library
- int[] arr1 = { 1, 2, 3, 4, 5 };
- int[] arr2= { 6, 7, 8, 9, 10 };
- int[] combArr = ArrayUtils.addAll(arr1 , arr2);
方法二
- // System.arraycopy()
- static String[] concat(String[] a, String[] b) {
- String[] c = new String[a.length + b.length];
- System.arraycopy(a, 0, c, 0, a.length);
- System.arraycopy(b, 0, c, a.length, b.length);
- return c;
- }
方法三
- //Arrays.copyOf()
- public static int[] concat(int[] first, int[] second) {
- int[] result = Arrays.copyOf(first, first.length + second.length);
- System.arraycopy(second, 0, result, first.length, second.length);
- return result;
- }
5. 逆向输出一个数组
方法一
- // Apache Commons Lang library
- int[] arr= { 1, 2, 3, 4, 5 };
- ArrayUtils.reverse(intArray);
- System.out.println(Arrays.toString(intArray));
- //[5, 4, 3, 2, 1]
方法二
- int[] arr = { 1, 2, 3, 4, 5 };
- int[] revArr = new int[arr.length];
- for(int i = 0; i < arr.length; i++){
- revArr[i] = arr[arr.length - i -1];
- }
- System.out.println(Arrays.toString(revArr));
- //[5, 4, 3, 2, 1]
6. 移除数组中的元素
- // Apache common lang
- int[] arr= { 1, 2, 3, 4, 5 };
- int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
- System.out.println(Arrays.toString(removed))
数组toString()方法,数组常用操作的更多相关文章
- day06 字典、元组、set的方法及常用操作
今日内容: 1.深浅拷贝 2.元组 3.字典 4.set 1.深浅拷贝 # 1.值拷贝 # 采用赋值的方法进行 # 只会将堆区容器变量与栈区的绑定关系进行复制 # 2.浅拷贝 # 会将堆区与栈区的绑定 ...
- php数组·的方法-数组与数据结构
/*数组与数据结构*/ //shuffle() 随机打乱数组 //array_push() 数组末尾添加元素 //array_pop() 数组末尾删除元素 //array_shift() 数组首位删除 ...
- linux shell数组赋值方法(常用)
http://blog.csdn.net/shaobingj126/article/details/7395161 Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... ...
- [Android L]SEAndroid开放设备文件结点权限(读或写)方法(涵盖常用操作:sys/xxx、proc/xxx、SystemProperties)
温馨提示 建议你先了解一下上一篇博文([Android L]SEAndroid增强Androd安全性背景概要及带来的影响)所讲的内容,先对SEAndroid窥个全貌,然后再继续本节内容. ...
- php数组·的方法-数组检索
/* * //数组检索函数 * */ //array_keys() 获取数组中所有键名 //array_values() 获取数组中所有键名 $arr6=range('a','e'); print_r ...
- ECMAScript 6中数组新方法
数组的方法 数组的的大部分方法都可以实现数组的遍历. foreach方法 实现数组的遍历 const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.forEach(fu ...
- selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码
目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...
- JavaScript数组常用操作
前言 相信大家都用惯了jquery或者underscore等这些类库中常用的数组相关的操作,如$.isArray,_.some,_.find等等方法.这里无非是对原生js的数组操作多了一些包装. 这里 ...
- javascript常见操作数组的方法
在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "obj ...
随机推荐
- 剑指offer——python【第37题】数字在排序数组中出现的次数
题目描述 统计一个数字在排序数组中出现的次数 思路 最贱的方法依旧是count计数.. 当然,,看到有序数组就应该想到二分法,找到重复数字左边和右边的数字,然后两个相减就可以了 解答 方法1 coun ...
- Java编程基础篇第五章
数组概述 概念:数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器.数组既可以存储基本数据类型,也可以存储引用数据类型.应用场景:为了存储同种数据类型的多个值 数组定义格式 格式1:元素类型 ...
- python全栈开发 * 08知识点汇总 * 180608
08知识点梳理 文件操作一 .文件操作 r (只读)1.r (读) rb(字节)f=open("果蔬大杂烩",mode="r",encoding="U ...
- allegro画元件封装
LP Wizard 10.5 根据标准,输入datasheet的尺寸,可以计算出推荐的焊盘和封装. 封装必须画的层: 1.引脚 2.pakage-> 2.1.assembly_top,add线( ...
- Echarts . 在柱状图中添加自定义值 (键值对)
x ["需求"] {"0":"使用Echarts根据数据加载一个饼状图"} {"1":"点击哪个饼状图,弹出此 ...
- the pitfull way to create a uClinux image including gdb.
After downloaded and installed the GCT's SDK and toolchain, we try to make an our own image which in ...
- 11.0-uC/OS-III就绪列表(优先级)
准备运行的任务被放置于就绪列表中.就绪列表包括2个部分:位映像组包含了优先级信息,一个表包含了所有指向就绪任务的指针. 1.优先级 图6-1到6-3显示了优先级的位映像组.它的宽度取决于CPU_DAT ...
- 为什么python中没有switch case语句
终于知道了python中映射的设计哲学. 我自己写的code : class order_status_switcher(object): def get_order_status(self,argu ...
- python if 和 else
money = input("请输入你在学校有多少钱?")if int(money) > 5000: print("这个月可以潇洒潇洒了,每天可以出去吃大餐了.&q ...
- [js]js原型链继承小结
这是之前总结的, 发现有很多的毛病,就是重点不突出,重新翻看的时候还是得耗费很长时间去理解这玩意. js中的继承 js中什么是类 1,类是函数数据类型 2.每个类有一个自带prototype属性 pr ...