Search code examples
javaarraysswingjoptionpane

How can I print an array in JOptionPane?


For example, I have an array of type int and size 3, it has elements 1, 2, 3 When I try to print it in JOptionPane with loops, it makes three different panes.

When I try:

JOptionPane.showMessageDialog( null, array );

It gives garbage values.

I have searched everywhere but I couldn't find a solution. How do I display the array in an option pane?


Solution

  • Use Arrays.toString () as :

        int [] arr = new int [3];
        arr[0]=1;
        arr[1]=2;
        arr[2]=3;
    
        JOptionPane.showMessageDialog(null, Arrays.toString(arr));