zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    常见名词解释
    主板结构解析
    计算机网络原理的总结
    Nginx的介绍
    优雅的python
    python小技巧
    python列表小程序
    学会浏览器查东西
    列表推导式
    深度优先算法与广度优先算法
  • 原文地址:https://www.cnblogs.com/E-star/p/3441169.html
Copyright © 2011-2022 走看看