• 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
    

      

  • 相关阅读:
    bash的shebang行
    Step By Step(Lua调用C函数)
    [Android问答] px、dp和sp,这些单位有什么区别?
    Android中Application类用法
    Android中SharedPreferences用法
    Android中SQLite用法
    SQLite应用实例
    Android中Parcelable接口用法
    iOS block 基本用法及代替代理
    Xcode插件
  • 原文地址:https://www.cnblogs.com/E-star/p/3441169.html
Copyright © 2020-2023  润新知