1. 程式人生 > >Arrays類中sort,binarySearch,fill例項

Arrays類中sort,binarySearch,fill例項

import java.util.Arrays.*;
public class Array {
	public static void main(String [] args)
	{
		double[] numbers={6.0,4.4,1.9,2.9,3.4,3.5};
		java.util.Arrays.sort(numbers);
		for(int i=0;i<numbers.length;i++)
			System.out.print(numbers[i]+" ");
		System.out.println();
		
		char[] chars={'a','A','4','F','D','P'};
		java.util.Arrays.sort(chars,1,3);//從chars【1】到chars【3-1】排序
		System.out.println(chars);
		int[] list={2,4,7,10,11,45,50,59,60,66,69,70,79};
		System.out.println("(1)index is "+java.util.Arrays.binarySearch(list,11));
		System.out.println("(2)index is "+java.util.Arrays.binarySearch(list, 12));
		
		char[] chars2={'a','c','g','x','y','z'};
		System.out.println("(3)index is "+java.util.Arrays.binarySearch(chars2,'a'));
		System.out.println("(3)index is "+java.util.Arrays.binarySearch(chars2, 't'));
		
		int[] list1={2,4,7,10};
		int[] list2={2,4,7,10};
		int[] list3={4,2,7,10};
		System.out.println(java.util.Arrays.equals(list1, list2));
		System.out.println(java.util.Arrays.equals(list2,list3));
		java.util.Arrays.fill(list1, 5);
		java.util.Arrays.fill(list2,1,3,8);//從chars【1】到chars【3-1】排序
		displayList(list1);
		displayList(list2);
	}
	public static void displayList(int[] list)
	{
		for(int i=0;i<list.length;i++)
		{
			System.out.print(list[i]+" ");
		}
		System.out.println();
	}
}