集合数据的直接打印输出

对于数组、List、Set和Map等数据容器类,数据要想打印输出:

直接输出数组对象得到内存地址hash值,可以使用Arrays.toString(数组对象)方法;

直接输出List、Set和Map得到元素内容,既可以直接 System.out.println(集合对象);

import java.util.*;

public class PrintCollection {

    public static void main(String[] args) {
        printArray();
        printList();
        printSet();
        printMap();
    }
    public static void printArray(){
        Object[] array=new Object[4];
        array[0]=123;
        array[1]="hello";
        array[2]=false;
        array[3]=new Date();
        System.out.println("Array:"+array);
        System.out.println("Array:"+ Arrays.toString(array));
    }

    public static void printList(){
        List<Object> list=new ArrayList<>();
        list.add(123);
        list.add("hello");
        list.add(false);
        list.add(new Date());
        System.out.println("List:"+list);
    }

    public static void printSet(){
        Set<Object> set=new HashSet<>();
        set.add(123);
        set.add("hello");
        set.add(false);
        set.add(new Date());
        System.out.println("Set:"+set);
    }

    public static void printMap(){
        Map<String,Object> map=new HashMap<>();
        map.put("number",123);
        map.put("string","hello");
        map.put("boolean",false);
        map.put("date",new Date());
        System.out.println("Map:"+map);
    }
}

运行结果:

Array:[Ljava.lang.Object;@735f7ae5
Array:[123, hello, false, Fri Jun 14 15:03:29 GMT+08:00 2019]
List:[123, hello, false, Fri Jun 14 15:03:29 GMT+08:00 2019]
Set:[Fri Jun 14 15:03:29 GMT+08:00 2019, false, 123, hello]
Map:{date=Fri Jun 14 15:03:29 GMT+08:00 2019, number=123, boolean=false, string=hello}

猜你喜欢

转载自blog.csdn.net/JWbonze/article/details/91975583
今日推荐