数组toString()方法,数组常用操作
来源:互联网 发布:无线访客网络 编辑:程序博客网 时间:2024/05/17 21:55
int[] arr ={1,2,3,4,5};String arrString = Arrays.toString(arr);//输出[I@7150bd4dSystem.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()的结果也是[类型@哈希值]。
所以数组转为字符串应写成:
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. 声明一个数组
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); // true4. 连接两个数组
方法一
//使用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 libraryint[] 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))
阅读全文
0 0
- 数组toString()方法,数组常用操作
- javascript操作数组常用方法
- javascript操作数组常用方法
- 浅谈JavaScript中数组操作常用方法
- 黑马程序员--数组操作的常用方法
- JavaScript中数组操作常用方法
- js数组操作的常用方法
- JavaScript常用数组操作函数方法
- JavaScript中数组操作常用方法
- 常用的几个操作数组的方法
- java的方法定义、数组定义、数组常用操作
- 数组不能通过toString方法转为字符串
- 数组不能通过toString方法转为字符串
- 使用toString()方法遍历数组
- js 常用数组操作
- 数组的常用操作
- JavaScript数组常用操作
- js 数组常用操作
- 攻破JAVA NIO技术壁垒
- 让MT7620完美支持32M SPI Flash(W25Q256) — 兼谈设备驱动中的shutdown方法
- 7种方法解决移动端Retina屏幕1px边框问题
- Luogu P1970 [NOIp提高组2013]花匠
- 人工智能都要进小学了,你还在等什么?
- 数组toString()方法,数组常用操作
- 网络请求+XListView展示数据
- 类的加载机制与反射学习笔记
- Divide and Conquer -- Leetcode problem169: Majority Element
- PAT 乙级练习题1011. A+B和C (15)
- 【construct-binary-tree-from-preorder-and-inorder-traversal】
- Android窗口管理分析(3):窗口分组及Z-order的确定
- ebay价格抓取 考虑remove 0results end
- 汽车加油问题