0

I have a question about printing differences between arrays and arrayLists.

Why am I able to use System.out.println(points) and System.out.println(intsArrayList); to print out the points stored in the arrayList and the Integer wrapper class, but I cannot do the same for points stored in arrays or integers stored in arrays? Why can I print the entire ArrayList using System.out.println but I can't print the entire array using System.out.println? The following is my code.

package practice;
import java.util.ArrayList;
import java.awt.Point;

class Main{
    public static void main(String[] args) {
        ArrayList<Point> points = new ArrayList<Point>();
        points.add(new Point(10,20));
        points.add(new Point(30,40));
        points.add(new Point(50,60));
        System.out.println(points);

        Point[] points1 = new Point[5];
        points1[0]=new Point(70,80);
        points1[1]=new Point(90,100);
        System.out.println(points1);

        int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; 
        System.out.println(intArray);

        ArrayList<Integer> intsArrayList = new ArrayList<Integer>();
        intsArrayList.add(new Integer(1));
        intsArrayList.add(new Integer(2));
        intsArrayList.add(new Integer(3));
        intsArrayList.add(new Integer(4));
        intsArrayList.add(new Integer(5));
        System.out.println(intsArrayList);


    }
}

The following is my output:

[java.awt.Point[x=10,y=20], java.awt.Point[x=30,y=40], java.awt.Point[x=50,y=60]]
[Ljava.awt.Point;@70dea4e
[I@5c647e05
[1, 2, 3, 4, 5]

edited for typo.

  • 1
    `ArrayList` has a `toString` method that outputs its contents. Arrays do not. You can use the static method [`Arrays.toString(myArray)`](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(int[])) for that. – khelwood May 16 '18 at 03:33
  • thanks ill take a look into toString() :) – csStudent7264 May 16 '18 at 03:40

0 Answers0