码迷,mamicode.com
首页 > 其他好文 > 详细

详解 Arrays类

时间:2020-03-04 23:07:44      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:方法   lis   boolean   目标   search   RoCE   rgs   code   克隆   

请关注本人博文——《详解 普通数组 —— Arrays类 与 浅克隆》

Arrays类:

概述:
针对数组进行操作的工具类。它提供了对于数组的值的排序、查找等功能。

现在,本人来展示一下Arrays类的常用API

  • public static List asList(T... a)
    返回一个受指定数组支持的固定大小的列表。
  • public static String toString(int[] a)
  • public static void sort(int[] a)
  • public static int binarySearch(int[] a,int key)
    这个方法的底层实现,就运用了二分查找的原理
  • static boolean equals(int[] a, int[] a2)
    比较两个数组中的元素,是否一样
  • static int[] copyOf(int[] original, int newLength)
    复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
  • static int[] copyOfRange(int[] original, int from, int to)
    复制旧数组中的指定范围间的几个元素到新数组中

现在,本人来展示下Arrays类的其余API

  • static void sort(八大基本类型[] a)
    对指定的 该数组按数字升序进行排序。
  • static void sort(Object[] a, int fromIndex, int toIndex)
    根据元素的自然顺序对指定对象数组的指定范围按升序进行排序。
  • static < T > List< T > asList(T... a)
    返回一个受指定数组支持的固定大小的列表。
  • static int binarySearch(八大基本类型[] a, 八大基本类型 key)
    使用二分搜索法来搜索指定数组,以获得指定的值。
  • static int binarySearch(八大基本类型[] a, int fromIndex, int toIndex, 八大基本类型 key)
    使用二分搜索法来搜索指定数组的范围,以获得指定的值。
  • static 八大基本类型[] copyOf(八大基本类型[] original, int newLength)
    复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
  • static T[] copyOf(T[] original, int newLength)
    复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
  • static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
    复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
  • static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
    复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
  • static 八大基本类型[] copyOfRange(八大基本类型[] original, int from, int to)
    将指定数组的指定范围复制到一个新数组。
  • static T[] copyOfRange(T[] original, int from, int to)
    将指定数组的指定范围复制到一个新数组。
  • static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
    将指定数组的指定范围复制到一个新数组。
  • static boolean equals(Object[] a, Object[] a2)
    如果两个指定的 Object[] 型数组彼此相等,则返回 true。
  • static String toString(八大基本类型[] a)
    返回指定数组内容的字符串表示形式。

至于排序,在本人的《数据结构与算法》专栏中已经详细的讲解过了,所以,在这里,本人来介绍一下针对数组的查找手段——二分查找
二分查找:
二分查找是有前提的:

前提
数组已经排序好

二分查找的思想是:

思想
将数组分为两半,让要目标值 与 中间的值作比较,比中间数大的话,就在中间数左边的序列中再这样查找;反之则在中间数右边的序列中这样查找

现在,我们来用代码简单实现下 二分查找:

private static int getIndex(int[] arr, int num) {
        //定义三个索引
        int minIndex = 0;
        int maxIndex = arr.length - 1;
        int centIndex = (minIndex + maxIndex) / 2;
        while (minIndex <= maxIndex) {
            if (num == arr[centIndex]) {
                return centIndex;
            } else if (num < arr[centIndex]) {
                maxIndex = centIndex - 1;
            } else if (num > arr[centIndex]) {
                minIndex = centIndex + 1;
            }
            //重新计算中间索引
            centIndex = (minIndex + maxIndex) / 2;
        }
        return -1;
    }

那么,本人在这里展示下Arrays类的使用:

package about_arrays;

import java.util.Arrays;
import java.util.List;

public class Test {
    
    public static void main(String[] args) {
        int[] array = new int[] {1, 3, 5, 7, 9, 11};
        
        List<Integer> list = Arrays.asList(1, 2, 5, 3, 4);
        
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        
        int position = Arrays.binarySearch(array, 7);
        System.out.println("7在array数组中的下标为:" + position);
    }
    
}

那么,本人来展示下运行结果:
技术图片

至于List,将在本人博文——《详解 List接口》讲到,希望大家多多支持!!!

(本人普通数组总集篇博文链接:https://www.cnblogs.com/codderYouzg/p/12416481.html

详解 Arrays类

标签:方法   lis   boolean   目标   search   RoCE   rgs   code   克隆   

原文地址:https://www.cnblogs.com/codderYouzg/p/12416489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!