Printing Elements of a List in Java
This question highlights an issue faced when attempting to print elements of a List in Java. The code provided, which utilizes a for loop to iterate through the elements and print them using list.get(i), yields the pointer of the object rather than the actual value.
To address this problem, it is recommended to use the following code to print elements of a List:
System.out.println(Arrays.toString(list.toArray()));
This code offers a concise way to print the elements without constructing a loop. However, it is important to note that if the objects in the List lack sensible toString() methods, the output will still display the object pointers (hash codes). This behavior is not specific to Lists but applies to objects in general.
The above is the detailed content of How do you print the elements of a Java List without getting object pointers?. For more information, please follow other related articles on the PHP Chinese website!