数组toString()⽅法,数组常⽤操作
int[] arr ={1,2,3,4,5};
String arrString = String(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() + '@' + HexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + HexString(hashCode());
}
⽽数组类中并没有对此⽅法重写(override),仅仅是重载(overload)为类的静态⽅法(参见java.util.Arrays)。所以,数组直接使⽤toString()的结果也是[类型@哈希值]。
所以数组转为字符串应写成:
这种⽅法的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 = String(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;
}
⽅法三
//pyOf()
public static int[] concat(int[] first, int[] second) {
int[] result = pyOf(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 };
System.out.String(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.String(revArr));
//[5, 4, 3, 2, 1]
6. 移除数组中的元素
// Apache common lang
int[] arr= { 1, 2, 3, 4, 5 };
int[] removed = veElement(intArray, 3);//create a new array System.out.String(removed))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论