java.Until.Arrays


Arrays

也是一个经常用到的类库,这个类库从意思来说处理数组的,有大量的方法


总结不佳,后续再补充和添加修改

Arrays一些方法

主要处理8种数据类型等各种发放操作,面对数组的
1.asList(存放相关的数据的) 作用,可以创建或者返回该数组固定大小的列表(后面会讲到列表,存放数据的一种集合)

// 格式 List <8种类型的库> 自定义列表名 = Arrays.asList(存放的与前面相对应的数据类型);
  List <Integer> num = Arrays.asList(1,2,3);

2.1 binarySerch(数据类型 [] 数据名 , 数据类型 key) 查找数组对应的key值返回是值数组的下标,没找到则返回一个负数。下面我们以int为例

 		int [] a ={1,3,5,7,9};
        System.out.println(Arrays.binarySearch(a,3)); // 结果为1

2.2 binarySerch(数据类型 [] 数据名 , int 数据下标1 ,int 数据下标2 ,数据类型 key) 也是查找,我们可以叫做范围查询,根据数组下标1-2,看在范围内是否有对应值,有返回对应下标值,没有则返回负数
注意: 1.数组下标1必须小于等于数组下标2,其他情况会报错
2.数组下标查询是左闭右开,也就是说从下标1比较到下标2的前一个,因此下标1从0开始取,下标2的最大范围是数组个数也就是长度

  1. compare这个也重载了方法,一种直接传入两个相同类型数组按字典顺序,
    还有一种(数据类型 [] 数据名1 , 数据1下标1 ,数据1下标2 ,数据类型2 [] 数据名 , 数据2下标1 ,数据2下标2 )按范围比较进行字典顺序,两个都一样的方法,只是后一个从范围开始比较
    关于字典顺序,分几种情况1.两个数组相等返回0,两个数组,不相等,
    2数据一样则统计多的不一样的作为返回值,代码详细解答,
    3开头不一样,这比较谁第一个大,前一个大返回1,前一个小返回-1
 int [] a ={1,3,5,7,9};
       int [] a1 ={1,3,5};
       System.out.println(Arrays.compare(a1,a)); // 输出为-2,如果a1和a交换输出为2,有相同的前一个减去后一个
       // 同样如果a1有与a不同的数直接进行大小 比较前一个大返回1,前一个小返回0
         int [] a ={1,3,5,7,9};
       int [] a1 ={1,3,5,4};
       System.out.println(Arrays.compare(a1,a));// 输出为-1,如果4改为8输出为1
         int [] a ={99,3,5,7,9};
       int [] a1 ={-59,3,5,4};
        System.out.println(Arrays.compare(a1,a)); // 输出只看两个数组第一个元素,a1比a小输出为-1

4.parallelSort(数组类型),sort(类型数组),将指定数组类型按照升降来进行排序,两个方法目前下面一样,但前者算法更好
下面原代码,方法的解释,打破数组到子数组自己排序使用合适的数组排序,数组小于等于最大值,该数组就直接使用原数组的大小排序

Sorts the specified array into ascending numerical order.
  *
  * @implNote The sorting algorithm is a parallel sort-merge that breaks the
  * array into sub-arrays that are themselves sorted and then merged. When
  * the sub-array length reaches a minimum granularity, the sub-array is
  * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
  * method. If the length of the specified array is less than the minimum
  * granularity, then it is sorted using the appropriate {@link
  * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
  * working space no greater than the size of the original array. The
  * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
  * execute any parallel tasks.
  *
  * @param a the array to be sorted
  *
  * @since 1.8

其他重写的方法类似,有按照下标指定排序,和各种不同类型的排序

  int [] a ={1,11,50,7,9};
     Arrays.parallelSort (a);
     for (int i = 0; i < a.length; i++) {
         System.out.print (a[i]+"  " );// 会按照从小到大输出 
     }

其他还有 equalsequals​(int[] a, int[] a2),里面两个数组相等返回true
fill​(int[] a, int val),将指定数组里面分配每一个数为val
mismatch​(int[] a, int[] b),查找两个数组第一个不相等的地方,返回字符索引,没有返回-1;
用到的也不多,还有toString(),HashCode()等

总结

Arrays,方便我们对数组进行一些操作,封装好的一些方法,我们将不用再去写相关数组查,改,删除方法了,总之以后使用数组可以考虑这些方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值