JAVA用Arrays为数组排序

导包

 使用java.util.Arrays类

     1.   java.util包提供的工具类

     2.   Arrays类提供操作数组的方法,如:排序、查询

     3.  Arrays类的sort()方法: 对数组进行升序排列

binarySearch 在数组中查找指定值(数组必须升序),找不到返回负数

        int scores[] = {2,20,90,8};
		Arrays.sort(scores);
		System.out.println(Arrays.toString(scores));
		int index = Arrays.binarySearch(scores, 101);
		System.out.println("index=="+index);

Arrays.copyOf  复制   浅拷贝(复制值)

        int numbers1[] = {1,2,3,4,5}; 
		int numbers2[] = Arrays.copyOf(numbers1,numbers1.length);
		System.out.println(numbers1==numbers2);
		System.out.println(Arrays.toString(numbers2));

fill  将数组中所有元素替换为指定内容

        String []names2 = {"张三","李四","王五"};
		System.out.println(Arrays.toString(names2));
		Arrays.fill(names2, "马六");
		System.out.println(Arrays.toString(names2));

toString  将数组转换为字符串

        String []names1 = {"张三","李四"};
		System.out.println(Arrays.toString(names1));


    equals 比较数组中值是否相等


		
		int []arr1 = {10,20,30,40};
		int []arr2 = {10,20,30,40};
		int []arr3 = {10,20,30};
		System.out.println(Arrays.equals(arr1, arr2));
		System.out.println(Arrays.equals(arr1,arr3));

sort 对数组进入升序排序

        sort 对数组进入升序排序
		int nums[] = {90,8,11,44,1}; //循环4轮   
		Arrays.sort(nums); //对数组进行升序
		System.out.println(Arrays.toString(nums));


      

     

你可能感兴趣的:(java,开发语言)