美文网首页
数组和容器的打印(学习java编程思想笔记)

数组和容器的打印(学习java编程思想笔记)

作者: 998584f56259 | 来源:发表于2016-10-24 11:37 被阅读13次

    对于数组的打印,必须使用Arrays.toString()方法来产生数组的的可表示打印,但是打印容器无需任何帮助。

    import java.util.Collection;
    import java.util.*;
    
    public class PrintingContainers {
    
    static Collection fill(Collection<String> collection) {
    collection.add("rat");
    collection.add("cat");
    collection.add("dog");
    collection.add("dog");
    return collection;
    }
    static Map fill(Map<String,String> map) {
    map.put("rat", "Fuzzy");
    map.put("cat", "Rags");
    //一个键只能对应一个值
    map.put("dog", "Bosco");
    map.put("dog", "Spot");
    return map;
    }   
    
    public static void main(String[] args) {
      
      System.out.println(fill(new ArrayList<String>()));
      System.out.println(fill(new LinkedList<String>()));
      
      //Set集合中不能保存相同元素
      System.out.println(fill(new HashSet<String>()));
      System.out.println(fill(new TreeSet<String>()));
      System.out.println(fill(new LinkedHashSet<String>()));
      
      System.out.println(fill(new HashMap<String,String>()));
      System.out.println(fill(new TreeMap<String,String>()));
      System.out.println(fill(new LinkedHashMap<String,String>()));
      
      int [] arg = {1,2,3,4};
      System.out.println(Arrays.toString(arg));
    }
    } /* Output:
    [rat, cat, dog, dog]
    [rat, cat, dog, dog]
    [dog, cat, rat]
    [cat, dog, rat]
    [rat, cat, dog]
    {dog=Spot, cat=Rags, rat=Fuzzy}
    {cat=Rags, dog=Spot, rat=Fuzzy}
    {rat=Fuzzy, cat=Rags, dog=Spot}
    [1, 2, 3, 4]
    */

    相关文章

      网友评论

          本文标题:数组和容器的打印(学习java编程思想笔记)

          本文链接:https://www.haomeiwen.com/subject/uchluttx.html