All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Print Array In Java

Last Updated : Mar 11, 2024

How To Print Array In Java

In this article we will show you the solution of how to print array in java, in Java programming, printing an array is a fundamental action that enables us to display the components of an array on the console or on any other output device.

A data structure called an array is useful for storing and managing data collections because it may hold several values of the same kind.

There are various ways to print an array in Java. To iterate over each element of the array & display each one separately, one typical technique is using a loop, which includes a for loop or a for each loop.

With this method, we have control over the printing format & may adjust the output to meet our demands.

In addition, Java offers useful classes like Arrays that come with preset methods to make array printing easier.

These handy techniques take the place of manual iteration and offer succinct answers for producing arrays of various data kinds. Now we’ll talk about the concept of printing an array in Java.

Step By Step Guide On How To Print Array In Java :-

import java.util.Arrays;
public class ArrayPrinter {
    public static void main(String[] args) {
        int[] numbers = { 111, 222, 333, 444, 555 };
        String arrayAsString = Arrays.toString(numbers);
        arrayAsString = arrayAsString.replaceAll("[\\[\\]]", "");
        System.out.println("The elements in the array are: " + arrayAsString);
        String[] words = { "talkerscode", "talkersmoney" };
        String wordsAsString = Arrays.toString(words);
        wordsAsString = wordsAsString.replaceAll("[\\[\\]]", "");
        System.out.println("The array's elements are: " + wordsAsString);
    }
}
  1. This Java code demonstrates how to print an array in Java, as you can see.
  2. We start by importing the Arrays class, which comes with utility techniques for working with arrays, from the java.util package.
  3. The main method is included in the ArrayPrinter class, which is defined after that. Our program's entrance point uses this technique.
  4. Declaring and setting the values 111, 222, 333, 444, and 555 for the integer array "numbers" These numbers are displayed in this array.
  5. We create a string representation of the numbers array using the Arrays.toString() method.
  6. This method returns an array of characters with the array elements enclosed in square brackets.
  7. The square brackets [] are then taken out of the string representation of the numbers array using the replaceAll() method.
  8. When using the regular expression [[]] as the first input to replaceAll(), we instruct the function to replace any instances of [or] with an empty string.
  9. The arrayAsString variable holds the updated string version of the numbers array sans the square brackets.
  10. We use the System.out.println() command to show the elements of the numbers array.
  11. We join the message together The arrayAsString variable, which holds the string representation of the numbers array, has the following array items.
  12. In a similar manner, we declare and initialize the variables "talkerscode" and "talkersmoney" in a string array named words.
  13. A list of words is represented by this array.
  14. Once more, we use the Arrays.toString() method to transform the word array into a string representation, this time enclosing the array components in square brackets.
  15. We remove the square brackets [] from the string representation of the words array using the replaceAll() method. The wordsAsString variable contains the updated string representation without the square brackets.
  16. We apply the System.out.println() command to display the words array's elements.
  17. We join the message together the words array's string representation is contained in the wordsAsString variable, which also holds the array's elements.

Conclusion :-

As a result, we have successfully acquired the knowledge necessary to print an array in Java.

We also learned how to use the replaceAll() function to get rid of undesirable characters like square brackets and the Arrays.toString() method to turn arrays into string representations.

These strategies, along with string manipulation techniques, enable the code to successfully show the array elements on the terminal.

In order to traverse through array elements as well as print them one at a time, it also introduces the idea of loops like the extended for loop as well as the while loop.

I hope this article on how to print array in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪