How To Print An Array In Java

Java can be a tricky programming language.

There’s a reason why many people prefer to use Python or even C, but if Java is your choice that doesn’t mean you should spend ages understanding how it works.

Today we will explain the basics of what an array is, what “print” is, and how to print an array using the Java programming language.

Advertising links are marked with *. We receive a small commission on sales, nothing changes for you.

What Is An Array?

How to print an array in Java

To understand an array, you need to be aware of variables.

Variables store the different values you can expect, however, one variable can only store one piece of data.

An array can also store data. It consists of multiple memory locations (which you can imagine as boxes), and each of those locations holds a single piece of data.

The difference is that each of these boxes of data share the same name.

For an array to work, the data has to be the same type.

Let’s use an example to visualize a variable and an array.

Say we are scoring a game that uses up to 10 scores.

Using the variable method, we could create this:

  • score_0
  • score_1
  • score_2
  • score_3
  • score_4
  • score_5
  • score_6
  • score_7
  • score_8
  • Score_9

Each variable is holding one value. The system would work, but it can take a long time to create – especially if this game needs more data.

Creating an array will make the process easier. Instead of having a variable for each store, we are going to have an array that holds everything.

It would look like this: score(9)

The number in the brackets refers to the number of data items the array can store.

In this case score(9) can store 10. This is because 0 is included.

Instead of counting from 1 to 10, the computer will start the count at 0 to 9.

What Is “Print”?

Just like a printer can send us information in a different format, the print function is a programming language that sends texts, objects, or variables to your screen.

For example, if you use the print command – System.out.println(“Hello world”); – you’ll receive an output that says “Hello world”.

So now you know what an array and printing are, it’s time to mix the two together.

The 4 Ways To Print An Array In Java 

We will start with the easiest and continue with more detailed options which give you more flexibility but can be tricky.

This list isn’t exhaustive, as there are many ways to print an array in Java, but these are the easiest to start you off.

A Simple Array

Let’s begin the process with a straightforward array.

To do this, you need to be aware of “Arrays.toString(arr)”. This will force your array to turn into a string.

Make a note of your objects and prepare for the input. Remember that an object is a variable, a function, data structure, or method. 

In our example, we want to print out the players in our game. Their names are Susan, Ali, and Mike.

This is what our simple array will look like:

String[] array = new String[] {“Susan”, “Ali”, “Mike”}; System.out.println(Arrays.toString(array));

The output will create a clean looking: [Susan, Ali, Mike]

Nested Array

Now you have the simple array, it’s time to get more complicated.

Nested arrays are arrays within arrays.

For example, Susan, Ali, and Mike might be on one team in this game, while Monique, Abudal, and Josh are on another team. 

We may want to group these people together while still showcasing them all.

To do that you should follow this example:

String[][] deepArray = new String[][] {{“Susa”, “Ali”, “Mike”}, {“Monique”, “Abdual”, “Josh”}}; System.out.println(Arrays.toString(deepArray)); 

//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] System.out.println(Arrays.deepToString(deepArray));

The output you will create is: [[Susan, Ali, Mike], [Monique, Abudal, Josh]]

Int Array

An integer array is a fancy way to say an array with whole numbers.

Int or integer is the computer programming name for a whole number, meaning something that doesn’t use fractions or decimals.

We could use this system to print out the seat numbers chosen for the game.

If there are 100 seats, each with its own numbered reference, we could use this system to see which ones have been used.

To do that, simply follow this example:

int[] intArray = { 9, 7, 3, 1, 5 }; 

System.out.println(Arrays.toString(intArray));

The outcome will be: [ 9, 7, 3, 1, 5 ];

Double Array

Double arrays are easily confused with two-dimensional arrays.

The 2-D version allows your computer to create shapes, but a double array allows your computer to store double data type values.

This means you can add decimal points to your array, creating more precise values.

Double arrays are the perfect choice for currency. 

Let’s go back to our game. An adult ticket will cost $5, while a child’s ticket will cost $2.50.

Knowing this we can create a double array, which can count these figures.

For example:

double[] doubleArray = { 5.0, 2.5 }; 

System.out.println(Arrays.toString(doubleArray));

The output will be: [ 5.0, 2.5 ]

Playing Around With The Data

Using these examples, you can pick the array which best suits your needs.

We have been using a game example, and as you can see, each example could be used in different settings, but not all settings could accept every option.

Counting currency, for example, would not be done using the int array.

Depending on your needs, figure out which array makes the most sense to you.

Summary

Each of the examples we have given is a simplified version of arrays.

You may want to create functions or objects which allow you to call every person in a team, without having to write down every person’s name.

This is possible, as long as you know how to call a function.

Knowing about the basics will give you a strong foundation to grow upon.

Each little detail can be used to create something new, so keep playing around with your scripts.

Advertising links are marked with *. We receive a small commission on sales, nothing changes for you.