基礎面試題:Arrays 工具類詳解(超詳細)

Aarry的幾個常用函數:

  1. 排序 : sort()
  2. 查找 : binarySearch()
  3. 比較: equals()
  4. 填充 : fill()
  5. 轉列表: asList()
  6. 轉字符串 : toString()
  7. 複製: copyOf()

排序 : sort()

1 void sort(Object[] o):對數組從小到大的排序(String類型的數組不宜使用)

2.void sort(int[] a, int fromIndex, int toIndex):對數組(from,to)從小到大的排序(String類型的數組不宜使用)

​ 只排(fromIndex,toIndex)之間的數值,且不包括fromIndex和toIndex。

3.void sort(T[] a, Comparator<? super T> c):類似Collections工具類的sort

4.void parallelSort(int[] a) 按照數字順序排列指定的數組(並行的)。同sort方法一樣也有按範圍的排序

		int a[] = { 1, 3, 2, 7, 6, 5, 4, 9 };
        // void sort(Object[] o):對數組從小到大的排序
        Arrays.sort(a);
        System.out.println("Arrays.sort(a):");
        for (int i : a) {
            System.out.print(i);
        }
        // 換行
        System.out.println();

        String[] strs = { "abc", "abd", "abf" };
        Arrays.sort(strs);
        System.out.println("Arrays.toString(strs)");
        for (String str : strs) {
            System.out.print(str);
        }
        // 換行
        System.out.println();

        // void sort(int[] a, int fromIndex, int toIndex):對數組(from,to)從小到大的排序
        //只排(fromIndex,toIndex)之間的數值,且不包括fromIndex和toIndex。
        int b[] = a.clone();
        Arrays.sort(b, 2, 6);
        System.out.println("Arrays.sort(b, 2, 6):");
        for (int i : b) {
            System.out.print(i);
        }
        // 換行
        System.out.println();

       // void sort(T[] a, Comparator<? super T> c):類似Collections工具類的sort
        Integer[] c=new Integer[]{1, 3, 2, 7, 6, 5, 4, 9};
        Arrays.sort(c,new Comparator<Integer>()
                {

                    public int compare(Integer o1, Integer o2)
                    {
                        return o2-o1;
                    }


                    public boolean equals(Object obj)
                    {
                        return false;
                    }
                });
        System.out.println("Arrays.sort(b, new Comparator<Integer>):");
        for (Integer integer:c)
        {
            System.out.print(integer);
        }

        // 換行
        System.out.println();

        // parallelSort(int[] a) 按照數字順序排列指定的數組(並行的)。同sort方法一樣也有按範圍的排序
        int d[] = a.clone();
        Arrays.parallelSort(d);
        System.out.println("Arrays.parallelSort(d):");
        for (int i : d) {
            System.out.print(i);
        }
        // 換行
        System.out.println();

        //parallelSort給字符數組排序,sort也可以
        char e[] = { 'a', 'f', 'b', 'c', 'e', 'A', 'C', 'B' };
        Arrays.parallelSort(d);
        System.out.println("Arrays.parallelSort(e):");
        for (char ch : e) {
            System.out.print(ch);
        }
        // 換行
        System.out.println();

輸出結果:

Arrays.sort(a):
12345679
Arrays.toString(strs)
abcabdabf
Arrays.sort(b, 2, 6):
12345679
Arrays.sort(b, new Comparator<Integer>):
97654321
Arrays.parallelSort(d):
12345679
Arrays.parallelSort(e):
afbceACB

查找 : binarySearch()

​ 1.int binarySearch(Object[] a, Object key):在a數組中查找key,返回key在數組中的位置

​ 二分查找法找指定元素的索引值,數組一定是排好序的,否則會出錯。找到元素,只會返回最後一個位置 。

​ 2.int binarySearch(int[] a, int fromIndex, int toIndex, int key):在a數組中查找key,返回key在數組中的位置[fromIndex,toIndex)(不包括toIndex)

int arr[] = { 1, 3, 2, 7, 6, 5, 4, 9 };
        //int binarySearch(Object[] a, Object key):在a數組中查找key,返回key在數組中的位置 (無序)
        int a1=Arrays.binarySearch(arr,10);
        int a2=Arrays.binarySearch(arr,6);
        System.out.println("not sort arr:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }
        System.out.println();
        System.out.println("search a1, a2:");
        System.out.print("a1:"+a1+",a2:"+a2);

        // 換行
        System.out.println();
        Arrays.sort(arr);
        //int binarySearch(Object[] a, Object key):在a數組中查找key,返回key在數組中的位置 (有序)
        a1=Arrays.binarySearch(arr,10);
        a2=Arrays.binarySearch(arr,6);
        System.out.println("sort arr:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }
        System.out.println();
        System.out.println("search a1, a2:");
        System.out.print("a1:"+a1+",a2:"+a2);
        System.out.println();

        //int binarySearch(int[] a, int fromIndex, int toIndex, int key):在a數組中查找key,返回key在數組中的位置[fromIndex,toIndex)
        a1=Arrays.binarySearch(arr,0,4,6);
        a2=Arrays.binarySearch(arr,0,6,6);
        System.out.println("search index a1, a2:");
        System.out.print("a1:"+a1+",a2:"+a2);
        System.out.println();

輸出結果:

not sort arr:
1,3,2,7,6,5,4,9,
search a1, a2:
a1:-9,a2:-4
sort arr:
1,2,3,4,5,6,7,9,
search a1, a2:
a1:-9,a2:5
search index a1, a2:
a1:-5,a2:5

比較: equals()

​ boolean equals(Object[] a, Object[] a2):比較兩個數據是否相等,如果兩個數組引用都是null,則它們被認爲是相等的

		//boolean equals(Object[] a, Object[] a2):比較兩個數據是否相等
        String string1[]={"1","2","3","4","5"};
        String string2[]={"1","3","5","7","9"};
        String string3[]={"1","3","5","7","9"};
        String string4[]=null;
        String string5[]=null;

        boolean b1= Arrays.equals(string1,string2);
        boolean b2=Arrays.equals(string2,string3);
        boolean b3=string2.equals(string3);
        boolean b4=Arrays.equals(string1,string3);
        boolean b5=Arrays.equals(string4,string5);

        System.out.println("b1:"+b1+",b2:"+b2+",b3:"+b3+",b4:"+b4+",b5:"+b5);

輸出結果:

b1:false,b2:true,b3:false,b4:false,b5:true

填充 : fill()

		1. void fill(Object[] a, Object val):填充數組
		2. void fill(Object[] a, int fromIndex, int toIndex, Object val):填充數組[fromIndex,toIndex)
		int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        // 數組中所有元素重新分配值
        Arrays.fill(a, 3);
        System.out.println("Arrays.fill(a, 3):");
        for (int i : a) {
            System.out.print(i);
        }
        // 換行
        System.out.println();

        int[] b = a.clone();
        // 數組中指定範圍元素重新分配值
        Arrays.fill(b, 0, 2, 9);
        System.out.println("Arrays.fill(b, 0, 2, 9):");
        for (int i : b) {
            System.out.print(i);
        }

輸出結果:

Arrays.fill(a, 3):
333333333
Arrays.fill(b, 0, 2, 9):
993333333

轉列表 asList()

​ List asList(T… a):將數組轉換成集合

		int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        // 數組中所有元素重新分配值
        System.out.println("int[] a:");
        for(int i:a){
            System.out.print(i);
        }
        System.out.println();

        List<int[]> list=Arrays.asList(a);
        Iterator<int[]> iterator=list.iterator();

        System.out.println("List<int[]> list:");
        while (iterator.hasNext()){
            int []result=iterator.next();
            for(int i:result){
                System.out.print(i);
            }
        }

輸出結果:

int[] a:
123456789
List<int[]> list:
123456789

轉字符串 toString()

​ 返回指定數組的內容的字符串表示形式

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

        System.out.println("Arrays.toString(a):");
        System.out.println(Arrays.toString(a));

輸出結果:

Arrays.toString(a):
[1, 2, 3, 4, 5, 6, 7, 8, 9]

複製 copyOf()

​ 1.int[] copyOf(int[] original, int newLength):從original數組中截取長度爲newLength的新數組

​ 2.int[] copyOfRange(int[] original, int from, int to):從original數組中截取[from,to)新數組

		int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        //copyOf 方法實現數組複製,a爲數組,6爲複製的長度
        int a1[] = Arrays.copyOf(a, 6);
        System.out.println("Arrays.copyOf(a, 6):");
        for (int i : a1) {
            System.out.print(i);
        }
        // 換行
        System.out.println();
        // copyOfRange將指定數組的指定範圍複製到新數組中
        int a2[] = Arrays.copyOfRange(a, 4, 8);
        System.out.println("Arrays.copyOfRange(a, 4, 8):");
        for (int j : a2) {
            System.out.print(j);
        }
        // 換行
        System.out.println();

輸出結果:

Arrays.copyOf(a, 6):
123456
Arrays.copyOfRange(a, 4, 8):
5678

如果大家對java架構相關感興趣,可以關注下面公衆號,會持續更新java基礎面試題, netty, spring boot,spring cloud等系列文章,一系列乾貨隨時送達, 超神之路從此展開, BTAJ不再是夢想!

架構殿堂

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章