Java学习笔记:Arrays

来源:互联网 发布:钣金外壳 软件 编辑:程序博客网 时间:2024/05/20 07:15

import java.util.Arrays;  


//  public static int binarySearch(byte[] a, byte key);

        // 使用二分法查询key元素值在a数组中出现的索引;

        // 如果a数组不包含key元素值,则返回负数(-0-1)。调用该方法时要求数组中元素已经按升序排列,否则出错

        //  如果byte[] 中含有多个 key则不能确定是哪一个key被找到

       int[] types1 =newint[]{1,2, 3, 4,4, 4,5, 6, 7,8, 9,10};

       int idx = Arrays.binarySearch(types1,4);

        System.out.println(idx);

       //  5

        

        

        //  public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key);

        // 使用二分法查询key元素在a数组中fromIndextoIndex索引的元素,调用这个方法时要求数组中元素已经按升序排列好,否则出错。

        //  如果a数组中范围fromIndextoIndex不包含key元素值,则返回负数(-fromIndex-1).

        //  如果byte[]fromIndextoIndex范围中包含多个key,则不能确定是哪一个key被找到

        //  如果 fromIndex > toIndex  --> IllegalArgumentException

        //  如果 fromIndex < 0 || toIndex > types2.length  -->  ArrayIndexOutOfBoundsException

       int[] types2 =newint[]{1,2, 3, 4,5, 6,7, 8, 8,9, 9,9, 10};

        idx = Arrays.binarySearch(types2,2,10, 5);

        System.out.println(idx);

       //  4

        

        

        //  public static byte[] copyOf(byte[] original, int newLength);

        // 这个方法将会把original数组复制成一个新数组,其中newLength是新数组的长度,如果newLength小于original数组的长度,则新数组就是原数组的前面newLength个元素;如果newLength大于original数组的长度,则新数组的前面元素就是原数组的所有元素,后面补充0(数组型)、false(布尔型)或者null(引用型)

        //  如果newLength是负数(negative)的话  -->  NegativeArraySizeException

        //  如果original是空  -->  NullPointerException

        String[] strings =new String[]{"aaa","bbb","ccc","ddd","eee"};

        String[] copyStrings = Arrays.copyOf(strings,7);

       for (String s : copyStrings) {

            System.out.print(s +" ,");

        }

        System.out.println();

        //  aaa ,bbb ,ccc ,ddd ,eee ,null ,null ,

        

        

        //  public static byte[] copyOfRange(byte[] original, int from, int to);

        // 这个方法会把original数组复制成一个新数组,但这个方法只复制original数组的from索引到to(不包括to)索引的元素,如果to大于original数组的长度,则在后面补充0(数组型)false(布尔型)、或则null(应用型)

        //  如果from < 0 或则 from > original.length  -->  ArrayIndexOutOfBoundsException

        //  如果from > to  -->  IllegalArgumentException

        //  如果original == null  -->  NullPointerException

       char[] chars = {'a','b','c', 'd', 'e', 'f'};

       char[] copyChars = Arrays.copyOfRange(chars,3, 10);

       for (char c : copyChars) {

            System.out.print(c +", ");

        }

        System.out.println();

        //  d, e, f, , , , ,

        

        

        //  public static boolean equals(byte[] a, byte[] a2);

        // 如果a数组和a2数组的长度相等,而且a数组和a2数组的数组元素也一一相同,该方法将返回true

        //  如果a数组和a2数组都为null,该方法也返回true

        String[] sArray1 =new String[]{"aaa","bbb","ccc"};

        String[] sArray2 =new String[]{"aaa","bbb","ccc"};

       boolean b = Arrays.equals(sArray1, sArray2);

        System.out.println(b);

       //  true

        Person[] p1 =new Person[1];

        Person[] p2 =new Person[1];

        Person xiaoming =new Person();

        xiaoming.age =10;

        xiaoming.height =20;

        p1[0] = xiaoming;

        Person xiaohong =new Person();

        xiaohong.age =10;

        xiaohong.height =20;

        p2[0] = xiaohong;

        b = Arrays.equals(p1, p2);

        System.out.println(b);

        //  false

        

        

        //  public static void fill(byte[] a, type val);

        // 该方法将会把a数组所有元素值都赋值为val

        String[] sArray =new String[5];

        Arrays.fill(sArray,"HelloWorld!");

       for (String s : sArray) {

            System.out.println(s);

        }

        //  HelloWorld!

        //  HelloWorld!

        //  HelloWorld!

        //  HelloWorld!

        //  HelloWorld!

        

        

        //  public static void fill(byte[] a, int fromIndex, int toIndex, byte val);

        //  该方法会把a数组中从fromIndextoIndex(不包括toIndex)索引之间的元素赋值为val

        //  如果fromIndex > toIndex  -->  IllegalArgumentException

        //  如果fromIndex < 0 或者 toIndex > a.length  -->  ArrayIndexOutOfBoundsException

       boolean[] bArrays = {true,false,false,true,true,false};

        Arrays.fill(bArrays,0, bArrays.length -1, true);

       for (boolean bool : bArrays) {

            System.out.print(bool +", ");

        }

        System.out.println();

        //  true, true, true, true, true, false,

        

        

        

        //  public static void sort(byte[] a);

        // 该方法对a数组的数组元素进行排序(升序)

       int[] iArrays = {10,9,8, 7, 6, 5,4,3, 2, 1};

        Arrays.sort(iArrays);

       for (int a : iArrays) {

            System.out.print(a +", ");

        }

        System.out.println();

        //  1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

        

        

        //  public static void sort(byte[] a, int fromIndex, int toIndex);

        //  该方法会把a数组中从fromIndextoIndex(不包括toIndex)索引之间的元素进行升序排列

        //  如果fromIndex > toIndex  -->  IllegalArgumentException

        //  如果fromIndex < 0 或者 toIndex > a.length  -->  ArrayIndexOutOfBoundsException

       float[] fArrays = {10.10f,9.9f,8.8f,7.7f, 6.6f,5.5f, 4.4f,2.2f, 1.1f,3.3f};

        Arrays.sort(fArrays,3, fArrays.length);

       for (float f : fArrays) {

            System.out.print(f +", ");

        }

        System.out.println();

        //  10.1, 9.9, 8.8, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7,

        

        

        //  public static String toString(byte[] a);

        // 该方法将一个数组转换成一个字符串,该方法按顺序把多个数组元素连缀在一起,多个数组元素使用英文逗号(,)和空格隔开,字符串两端用[]包起来

        //  如果数组为空,则返回[]

       boolean[] bArrays1 = {false,true,true,false,true};

        String s = Arrays.toString(bArrays1);

        System.out.println(s);

        //  [false, true, true, false, true]

0 0