zoukankan      html  css  js  c++  java
  • Java中打印数组内容的方式有哪些?

    下面是几种常见的打印方式。

    方法一:使用循环打印。

     public class Demo {
        public static void main(String[] args) {
            String[] infos = new String[] {"Java", "Android", "C/C++", "Kotlin"};

            StringBuffer strBuffer = new StringBuffer();
            for(int i = 0; i< infos.length; i++) {
                if(i > 0) {
                    strBuffer.append(", ");
                }
                strBuffer.append(infos[i]);
            }
            System.out.println(strBuffer);
        }
     
     
     
    方法二:使用 Arrays.toString() 打印。
     
     public class Demo {
        public static void main(String[] args) {
            String[] infos = new String[] {"Java", "Android", "C/C++", "Kotlin"};

            System.out.println(Arrays.toString(infos));
        }
     
     
     
    方法三:使用 JDK8 的  java.util.Arrays.stream()  打印。
     
     public class Demo {
        public static void main(String[] args) {
            String[] infos = new String[] {"Java", "Android", "C/C++", "Kotlin"};

            Arrays.stream(infos).forEach(System.out::println);
        }
     
     
     
    方法四:使用 Arrays.deepToString() 方法打印。如果数组中有其它数组,即多维数组,也会用同样的方法深度显示。
     
     public class Demo {
        public static void main(String[] args) {
            String[] infos = new String[] {"Java", "Android", "C/C++", "Kotlin"};

            System.out.println(Arrays.deepToString(infos));
        }
     
     
     
    方法五:使用 JDK 8 的 Stream.flatMap() 打印。
     
     public class Demo {
        public static void main(String[] args) {
            String[] infos = new String[] {"Java", "Android", "C/C++", "Kotlin"};

            Arrays.stream(infos).forEach(System.out::println);
        }
     
     
     
  • 相关阅读:
    如何制定一周工作计划
    如何评估工作offer(1)
    Iraq shoethrower inspires Web games
    数据加密技术
    数字签名技术原理
    [转载]CSS使用技巧大全
    数字签名介绍
    数字签名原理剖析
    6个有用的MySQL语句
    PGP概述及原理
  • 原文地址:https://www.cnblogs.com/yuwenweisan/p/10025668.html
Copyright © 2011-2022 走看看