Java中Arrays 与 Collections 的简单操作

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collection;

import java.util.Collections;

import java.util.List;





public class Test{





	

		

	public static void main(String[] args) {

		

		/*

		 * Arrays 提供了很多对数组操作的函数

		 * 这里只演示几个常用功能呢个

		 */

		

		System.out.println("-----------Arrays--------------");

		String[] s = new String[10];

		Arrays.fill(s, "a");

		System.out.println("Arrays.fill :  " + Arrays.deepToString(s));

		s = new String[]{"Jone","tom", "jerry"};

		System.out.println("Arrays.deepToString : " + Arrays.deepToString(s));

		Arrays.sort(s);

		System.out.println("Arrays.sort : " + Arrays.deepToString(s));

		System.out.println("Arrays.binarySearch : " + Arrays.binarySearch(s, "jerry"));

	

		/*

		 * Collections 提供了很多对集合操作的函数

		 * 这里只演示几个常用功能呢个

		 */

		System.out.println("-----------Collections--------------");

		List<String> s1 = new ArrayList<String>();

		Collections.fill(s1, "bb");

		System.out.println("Collections.fill bb : " + s1);

		s1.add("Jone"); s1.add("tom");  s1.add("jerry"); 

		Collections.fill(s1, "cc");

		System.out.println("Collections.fill bb : " + s1);  //只能替换已存在的

		s1.clear();

		s1.add("Jone"); s1.add("tom");  s1.add("jerry"); 

		System.out.println(s1);

		

		Collections.sort(s1);

		System.out.println("Collections.sort : " + s1);

		System.out.println("Collections.binarySearch : " + Collections.binarySearch(s1, "jerry"));

		

		System.out.println("Collections.max : " + Collections.max(s1) + "Collections.min :" + Collections.min(s1));

	}



}



输出:

-----------Arrays--------------

Arrays.fill :  [a, a, a, a, a, a, a, a, a, a]

Arrays.deepToString : [Jone, tom, jerry]

Arrays.sort : [Jone, jerry, tom]

Arrays.binarySearch : 1

-----------Collections--------------

Collections.fill bb : []

Collections.fill bb : [cc, cc, cc]

[Jone, tom, jerry]

Collections.sort : [Jone, jerry, tom]

Collections.binarySearch : 1

Collections.max : tomCollections.min :Jone

  

 

你可能感兴趣的:(Collections)