数组常用API之对对象的方法

arr.clone()

通过克隆生成数组

public class cloneTest {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        int[] brr= arr.clone();
        for(int item : brr){
            System.out.println(item +" ");
        }
    }
}

arr.equal()

对比Arryays.equal()

import java.util.Arrays;

public class equalTest {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6};
        int[] b=a.clone();
        int[] c=a;
        System.out.println(a.equals(b)); //false  因为a.equal比较的是地址
        System.out.println(Arrays.equals(a,b));  //true   Arrays.equal 对比的是内容
        System.out.println(a.equals(c));  //true
    }
}

arr.getClass()

public class getClassTest {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6};
        System.out.println(a.getClass());
    }

}

运行结果是:

class [I

一脸懵X,继续找资料吧

import java.util.GregorianCalendar;
class ObjectDemo {
    public static void main(String[] args) {

        // create a new ObjectDemo object
        GregorianCalendar cal = new GregorianCalendar();

        // print current time
        System.out.println("" + cal.getTime());

        // print the class of cal
        System.out.println("" + cal.getClass());

        // create a new Integer
        Integer i = new Integer(5);

        // print i
        System.out.println("" + i);

        // print the class of i
        System.out.println("" + i.getClass());
    }
}

运行结果:

Fri Jul 26 10:53:26 CST 2019
class java.util.GregorianCalendar
5
class java.lang.Integer

直观理解getClass()返回运行实例的class

a.hashCode()

import java.util.Arrays;

public class hashCodeTest2 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6};
        System.out.println(a.hashCode());
        System.out.println(Arrays.hashCode(a));

    }
}

a.toString()

import java.util.Arrays;

public class toStringTest {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6};
        System.out.println(a.toString());
        System.out.println(Arrays.toString(a));
    }
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页