array工具类的学习

日积月累, 滴水石穿。敲代码是极好的。。。
package com.gt.gtop.test.java8;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * arrays 类的应用
 * @author 非宁静无以致远
 *
 */
public class ArraysTest {
	//java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。
	public static void main(String[] args) {
		Integer [] array = {1,2,34,1322,2343,354,23,4};
		Integer [] array1 = {1,2,34,1322,2343,354,4,23};
		System.out.println("********asList************************");
		//asList   返回由指定数组支持的固定大小的列表。 
		List<Integer> asList = Arrays.asList(array);
		asList.stream().forEach(e -> System.out.println(e));
		
		System.out.println("********copyOf************************");
		//复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
		Integer[] copyOf = Arrays.copyOf(array, 2);
		Stream.of(copyOf).forEach(e -> System.out.println(e));
		
		System.out.println("********copyOfRange************************");
		//将指定数组的指定范围复制到新数组中。
		Integer[] copyOfRange = Arrays.copyOfRange(array, 1, 3);
		Stream.of(copyOfRange).forEach(e -> System.out.println(e));
		
		//如果两个指定的数组彼此 深度相等 ,则返回 true 。 
		//如果两个数组使用equals返回true,则使用deepEquals也返回true,
		//也就是说在比较的两个数组均为一维数组的前提下,equals和deepEquals的比较结果没有差别;
		//多维数组用deepEquals
		
		System.out.println("********deepEquals************************");
		boolean deepEquals = Arrays.deepEquals(array, array1);
		System.out.println(deepEquals);
		
		//如果两个指定的数组彼此 深度相等 ,则返回 true 。 
		//如果两个数组使用equals返回true,则使用deepEquals也返回true,
		//也就是说在比较的两个数组均为一维数组的前提下,equals和deepEquals的比较结果没有差别;
		
		System.out.println("********equals************************");
		boolean equals = Arrays.equals(array, array1);
		System.out.println(equals);
		
		System.out.println("********fill************************");
		//将指定的布尔值分配给指定的布尔数组的每个元素
		Arrays.fill(array1, 2);
		Stream.of(array1).forEach(e -> System.out.println(e));
		
		System.out.println("********parallelSort************************");
		//按照数字顺序排列指定的数组。
		Arrays.parallelSort(array);
		Stream.of(array).forEach(e -> System.out.println(e));

		//按照数字顺序排列指定的数组
		System.out.println("********sort************************");
		Arrays.sort(array);
		Stream.of(array).forEach(e -> System.out.println(e));
		
		//比较器
		System.out.println("********sort************************");
		Arrays.sort(array, new  Comparator<Integer>(){
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2 - o1;
			};
		});
		Stream.of(array).forEach(e -> System.out.println(e));

		//返回指定数组的内容的字符串表示形式
		System.out.println("********string************************");
		String string = Arrays.toString(array);
		System.out.println(string);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值