How to output array values without using a for statement

When you want to display an array on the console in Java, you used to write a for statement etc., but you can easily display it by using the Arrays.toString method.

Reference page

sample



int[] a = {1, 2};	

//So far
for (int i: a) {
	System.out.println(i);
}
//Or
Arrays.stream(a).forEach(System.out::println);

//from now on
System.out.println(Arrays.toString(a));

//Caution
System.out.println(a.toString());

console


1
2
1
2
[1, 2]
[I@bebdb06

Comparing the above three things, the Arrays.toString method is the cleanest. I think the method using Stream API is also good, but Arrays.toString is better because it is difficult to describe when dealing with multidimensional arrays.

By the way, if you do a.toString () like the last one, the hash value of the array variable will be displayed. Also, in standard output methods such as System.out.println, the argument toString () is called internally, so refrain from redundant writing such as System.out.println (sb.toString ()) ;. Let's do it.

For multidimensional arrays

Use the deepToString method.

String[][] sss = {{"a","bb"}, {"ccc"}};

System.out.println(Arrays.toString(sss));
System.out.println(Arrays.deepToString(sss));

console


[[Ljava.lang.String;@bebdb06, [Ljava.lang.String;@7a4f0f29]
[[a, bb], [ccc]]

Summary

If you want to output an array as standard, use the ** Arrays.toString ** and ** Arrays.deepToString ** methods.

Recommended Posts

How to output array values without using a for statement
How to make a groundbreaking diamond using Java for statement wwww
How to create pagination for a "kaminari" array
[Java] How to turn a two-dimensional array with an extended for statement
How to use an array for a TreeMap key
How to join a table without using DBFlute and sql
How to make a Java array
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to get the contents of Map using for statement Memorandum
How to execute a contract using web3j
How to sort a List using Comparator
How to add a new hash / array
How to create a Maven repository for 2020
How to get the latest live stream ID for a channel without using the YouTube Data API
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Java] [For beginners] How to insert elements directly in a 2D array
How to create a database for H2 Database anywhere
How to output Excel and PDF using Excella
[Rails] How to create a graph using lazy_high_charts
How to use an array for HashMap keys
How to delete a controller etc. using a command
[Ethereum] How to execute a contract using web3j-Part 2-
How to make a lightweight JRE for distribution
How to generate a primary key using @GeneratedValue
How to specify index of JavaScript for statement
[Java] How to get HashMap elements by loop control using extended for statement
How to make a judgment method to search for an arbitrary character in an array
How to update devise user information without a password
How to delete custom Adapter elements using a custom model
How to output standard from an array with forEach
How to convert A to a and a to A using AND and OR in Java
How to start tomcat local server without using eclipse
How to use Play Framework without using typesafe activator
How to convert a file to a byte array in Java
How to make a cache without thinking too much
How to make a mod for Slay the Spire
How to loop Java Map (for Each / extended for statement)
[Rails] How to install a decorator using gem draper
SDWebImage: How to clear the cache for a particular UIImageView
How to create a lightweight container image for Java apps
How to divide a two-dimensional array into four with ruby
How to change a string in an array to a number in Ruby
How to leave a comment
[For Ruby beginners] Explain how to freely delete array elements!
How to develop separately into Xib files without using Storyboard
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to add the same Indexes in a nested array
How to install JDK 8 on Windows without using the installer
Minecraft Modding [1.12] How to attach a special render for Item
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to initialize Java array
How to insert a video
How to create a method
How to create and launch a Dockerfile for Payara Micro
How to register as a customer with Square using Tomcat
How to authorize using graphql-ruby
How to control transactions in Spring Boot without using @Transactional
[Java] How to search for a value in an array (or list) with the contains method
How to create a jar file or war file using the jar command
How to run a mock server on Swagger-ui using stoplight/prism (using AWS/EC2/Docker)
[Rails 6] How to create a dynamic form input screen using cocoon