10 way to print array in Java

package ultima; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.google.gson.Gson; import java.io.IOException; import java.time.DayOfWeek; import java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; import java.util.Arrays; public class ArrayToString { public static void main(String[] args) throws IOException { String[] strArray = new String[]{"John", "Mary", "Bob"}; // #1 Arrays.asList(strArray).stream().forEach(s -> System.out.println(s)); // #2 Stream.of(strArray).forEach(System.out::println); // #3 Arrays.stream(strArray).forEach(System.out::println); // #4 System.out.println(Arrays.toString(strArray));

package ultima; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.google.gson.Gson; import java.io.IOException; import java.time.DayOfWeek; import java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; import java.util.Arrays; public class ArrayToString { public static void main(String[] args) throws IOException { String[] strArray = new String[]{"John", "Mary", "Bob"}; // #1 Arrays.asList(strArray).stream().forEach(s -> System.out.println(s)); // #2 Stream.of(strArray).forEach(System.out::println); // #3 Arrays.stream(strArray).forEach(System.out::println); // #4 System.out.println(Arrays.toString(strArray));
package ultima;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.gson.Gson;

import java.io.IOException;
import java.time.DayOfWeek;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.Arrays;

public class ArrayToString {

    public static void main(String[] args) throws IOException {

        String[] strArray = new String[]{"John", "Mary", "Bob"};
        // #1
        Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));
        // #2
        Stream.of(strArray).forEach(System.out::println);
        // #3
        Arrays.stream(strArray).forEach(System.out::println);
        // #4
        System.out.println(Arrays.toString(strArray));
        // #5
        // String[] strArray = new String[]{"John", "Mary", "Bob"};
        for (String n : strArray) {
            System.out.println(n);
        }
        // #6
        System.out.println(Arrays.deepToString(strArray).replaceAll("],", "]," + System.getProperty("line.separator")));
        // #7
        int[] intArray = new int[]{1, 2, 3, 4, 5};
        for (int i = 0; i < intArray.length; i++) {
            System.out.print(intArray[i] + ", ");
        }
        // #8

        //
        //    com.fasterxml.jackson.core
        //    jackson-databind
        //    2.9.8
        //
        // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
        //compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'

        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        System.out.println(ow.writeValueAsString(strArray));
        Gson gson = new Gson();
        System.out.println(gson.toJson(strArray));
        // #9
        int x[] = {1, 2, 3};
        String printableText = Arrays.toString(x).replaceAll("[\[\]]", "").replaceAll(", ", "n");
        System.out.println(printableText);
        // #10

        // int[] intArray = new int[]{1, 2, 3, 4, 5};
        // String[] strArray = new String[]{"John", "Mary", "Bob"};
        //Prior to Java 8
        System.out.println(Arrays.toString(intArray));
        System.out.println(Arrays.toString(strArray));
        // In Java 8 we have lambda expressions
        Arrays.stream(intArray).forEach(System.out::println);
        Arrays.stream(strArray).forEach(System.out::println);

        // #11
        int[] ints = new int[]{1, 2, 3, 4, 5};
        System.out.println(IntStream.of(ints).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
        System.out.println(IntStream.of(ints).boxed().map(Object::toString).collect(Collectors.joining(", ")));
        System.out.println(Arrays.toString(ints));
        String[] strs = new String[]{"John", "Mary", "Bob"};
        System.out.println(Stream.of(strs).collect(Collectors.joining(", ")));
        System.out.println(String.join(", ", strs));
        System.out.println(Arrays.toString(strs));
        String[] days = {"FRIDAY", "MONDAY", "TUESDAY"};
        System.out.println(Stream.of(days).map(Object::toString).collect(Collectors.joining(", ")));
        System.out.println(Arrays.toString(days));
        // These options are not the same as each item is printed on a new line:
        IntStream.of(ints).forEach(System.out::println);
        Stream.of(strs).forEach(System.out::println);
        Stream.of(days).forEach(System.out::println);

        // #12
        // Simple Array:
        String[] array = new String[]{"John", "Mary", "Bob"};
        System.out.println(Arrays.toString(array));
        // Output: [John, Mary, Bob]
        // Nested Array:
        String[][] deepArray = new String[][]{{"John", "Mary"}, {"Alice", "Bob"}};
        System.out.println(Arrays.toString(deepArray));
        //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
        System.out.println(Arrays.deepToString(deepArray));
        // Output:  [[John, Mary], [Alice, Bob]]
        // double Array:
        double[] doubleArray = {7.0, 9.0, 5.0, 1.0, 3.0};
        System.out.println(Arrays.toString(doubleArray));
        //Output:  [7.0, 9.0, 5.0, 1.0, 3.0 ]

        // int Array:
        // int[] intArray = {7, 9, 5, 1, 3};
        System.out.println(Arrays.toString(intArray));
        // Output: [7, 9, 5, 1, 3 ]
    }
}

http://tpcg.io/0czamzbb

Source: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments