Saturday, June 21, 2025
HomeJavaMethods to Print Array with parts in Java?

Methods to Print Array with parts in Java? [Solution + Example]


You can’t print array parts instantly in Java, you might want to use Arrays.toString() or Arrays.deepToString() to print array parts. Use toString() if you wish to print a one-dimensional array and use deepToString() technique if you wish to print a two-dimensional array. Have you ever tried printing arrays in Java earlier than? What did you do? simply handed an array to println() technique and anticipating it prints its parts? Me too, however surprisingly array regardless of being Object and offering a size discipline, would not appear overriding the toString() technique from java.lang.Object class. All it prints is kind@somenumber. This isn’t in any respect helpful for anybody who’s involved in seeing whether or not an array is empty or not, if not then what parts it has and so forth.

To print Java array in a significant means, you needn’t look additional as a result of your very personal Assortment framework gives a number of array utility strategies in java.util.Arrays class. Right here we’ve toString() and deepToString() technique to print array in Java.

These strategies are overloaded, very similar to System.out.println() technique to just accept all primitive sorts, which suggests a unique technique is named for those who cross a boolean array, and a unique one is named whenever you print integer array.

The identical is true with deepToString(), which is used to print two-dimensional arrays in Java. On this Java array tutorial, we’ll see examples of a printing string array, integer array, byte array and a two-dimensional array in Java. The remainder of them are like that, which suggests by following these examples, it’s best to be capable of print boolean, char, brief, float, double and lengthy array by your personal.

Methods to Print int array in Java – Examples

With the intention to print an integer array, all you might want to do is name Arrays.toString(int array) technique and cross your integer array to it. This technique will maintain the printing content material of your integer array, as proven beneath. In the event you instantly cross int array to System.out.println(), you’ll solely see the kind of array and a random quantity.

int[] primes = {5, 7, 11, 17, 19, 23, 29, 31, 37};
System.out.println("Prime numbers : " + primes);
System.out.println("Actual prime numbers : " + Arrays.toString(primes)); //Okay

Output:
Prime numbers : [I@5eb1404f
Real prime numbers : [5, 7, 11, 17, 19, 23, 29, 31, 37]

Methods to Print byte array in Java

the printing byte array is not any completely different than printing int array, as Arrays class gives an overloaded technique toString(byte[] bytes) to print contents of byte array in Java, as proven beneath. By the best way, if you wish to print byte array as Hex String then see this hyperlink.

String random = "In Java programming langue, array is object";
byte[] bytes = random.getBytes();
System.out.println("What's inside bytes : " + bytes);
System.out.println("Not seen, test intently .." + Arrays.toString(bytes));

Output
What's inside bytes : [B@31602bbc
Not visible, check closely ..[73, 110, 32, 74, 97, 118, 97, 32, 112, 
114, 111, 103, 114, 97, 109, 109, 105, 110, 103, 32, 108, 97, 
110, 97, 103, 117, 101, 44, 32, 97, 114, 114, 97, 121, 32, 105,
 115, 32, 111, 98, 106, 101, 99, 116]

Methods to Print String array in Java

Printing string array in Java might be the simplest factor to do as a result of Arrays class has one other overloaded model of toString() to just accept Object. This technique calls toString() of Object to get a printable String. This can be used to print the array of any arbitrary object in Java. The user-defined object should override the toString() technique to indicate one thing cheap on the console.

String[] buzzwords = {"Java", "Android", "iOS", "Scala", "Python"};
System.out.println("Buzzing .." + buzzwords);
System.out.println("Not buzzing? strive once more : " + Arrays.toString(buzzwords));

Output:
Buzzing ..[Ljava.lang.String;@46f5331a
Not buzzing? try again : [Java, Android, iOS, Scala, Python]

Methods to Print Two Dimensional Array in Java

how to print Array in Java with exampleArrays class gives a unique technique to print a two-dimensional array in Java, it’s referred to as toDeepString(). It is able to printing multi-dimensional array in Java and just like toDeepEquals() which is used to check multi-dimensional array in Java. This technique can be overloaded and gives 8 + 1 primitive and object variations to just accept boolean, byte, brief, char, int, lengthy, float, double and Object in Java. Right here is an instance of tips on how to print a two-dimensional array in Java.

String[][] telephones = {{"Apple", "iPhone"}, {"Samsung", "Galaxy"}, {"Sony", "Xperia"}};
System.out.println("Sizzling telephones .. " + telephones);
System.out.println("Not sizzling? See once more.." + Arrays.deepToString(telephones));

Output
Sizzling telephones .. [[Ljava.lang.String;@57398044
Not hot? See again..[[Apple, iPhone], [Samsung, Galaxy], [Sony, Xperia]]

Full Java Program to Print Array in Java

That is the total Java code of print several types of arrays in Java. As defined on this article, it prints integer, String, byte, and two-dimensional array utilizing toString() and deepToString() technique of java.util.Arrays class. 

You possibly can copy-paste this program in your Java IDE and run it. Do not want any third-party libraries.

import java.util.Arrays;
/**
 * Java Program to print arrays in Java. We'll discover ways to print String, int,
 * byte and two dimensional arrays in Java through the use of toString() and
 * deepToString() technique of Arrays class.
 *
 * @creator http://java67.blogspot.com
 */
public class PrintArrayInJava{
    
  public static void predominant(String args[]) {
       
        // Instance 1 : print int array in Java
        int[] primes = {5, 7, 11, 17, 19, 23, 29, 31, 37};
        System.out.println("Prime numbers : " + primes); // Not OK
        System.out.println("Actual prime numbers : " + Arrays.toString(primes)); //Okay
        
       // Instance 2 : print String array in Java
        String[] buzzwords = {"Java", "Android", "iOS", "Scala", "Python"};
        System.out.println("Buzzing .." + buzzwords);
        System.out.println("Not buzzing? strive once more : " + Arrays.toString(buzzwords));
        
        // Instance 3 : print two dimensional array in Java
        String[][] telephones = {{"Apple", "iPhone"}, {"Samsung", "Galaxy"},
                        {"Sony", "Xperia"}};
        System.out.println("Sizzling telephones .. " + telephones);
        System.out.println("Not sizzling? See once more.." + Arrays.deepToString(telephones));
       
       // Instance 4 : print byte array in Java
        String random = "In Java programming langue, array is object";
        byte[] bytes = random.getBytes();
        System.out.println("What's inside bytes : " + bytes);
        System.out.println("Not seen, test intently .." + Arrays.toString(bytes));
    }

}

Output:
Prime numbers : [I@5eb1404f
Real prime numbers : [5, 7, 11, 17, 19, 23, 29, 31, 37]
Buzzing ..[Ljava.lang.String;@46f5331a
Not buzzing? try again : [Java, Android, iOS, Scala, Python]
Sizzling telephones .. [[Ljava.lang.String;@57398044
Not hot? See again..[[Apple, iPhone], [Samsung, Galaxy], [Sony, Xperia]]
What's inside bytes : [B@31602bbc
Not visible, check closely ..[73, 110, 32, 74, 97, 118, 97, 32, 112, 
114, 111, 103, 114, 97, 109, 109, 105, 110, 103, 32, 108, 97, 
110, 97, 103, 117, 101, 44, 32, 97, 114, 114, 97, 121, 32, 105, 
115, 32, 111, 98, 106, 101, 99, 116]

And, here’s a good abstract of tips on how to print array in Java:

How to print array elements in Java

That is all about tips on how to print array in Java. Now we have realized tips on how to print objects of an array, as a substitute of an array object, which is nothing however a hashCode. I actually hope that Java ought to add a toString() in Array, as a substitute of offering Arrays.toString(), do not know after they selected different components. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments