Languages

Java - how to print array?

0 points
Asked by:
Alicia-Lambert
340

What is the easiest way to print an array in Java? How can I get the proper output?

I tried this:

public class Example {

    private static int[] myArray = {1, 2, 3};

    public static void main(String[] args) {
        System.out.println(myArray);
    }
}

but I got this:

[I@e580929
1 answer
0 points
Answered by:
Alicia-Lambert
340

Import:

import java.util.Arrays;

and use Arrays.toString() method to print the array, so your code would look like this:

import java.util.Arrays;

public class Example {

    private static int[] myArray = {1, 2, 3};

    public static void main(String[] args) {
        System.out.println(Arrays.toString(myArray));
    }
}

Output:

[1, 2, 3]

 

0 comments Add comment
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join