Java Print Array Without Brackets

Java Print Array Without Brackets | In this section, we print the array without brackets and commas to do this we use replace() method and the Arrays.toString() method available in Java. See the below examples to understand the problem:-

1) array[ ] = {1, 2, 3, 4, 5}
Print array without brackets:-
1, 2, 3, 4, 5

2) array[ ] = {1, 2, 3, 4, 5}
Print an array without commas and brackets:-
1 2 3 4 5

3) array[ ] = { “Java”, “Programming”, “Language” }
Java Programming Language

How to Print an Array Without Brackets Java

In the Java Arrays class, the toString() method is given to convert the array to a string. While converting the array to string it adds square brackets around the result. To remove them from the string we can use replace() method of the Java String class.

Program to demonstrate Java how to print elements of an array without brackets

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      int array[] = { 1, 2, 3, 4, 5 };
      for (int i = 0; i < 1; i++) {
         System.out.println(Arrays.toString(array));
         System.out.println(Arrays.toString(array)
                                  .replace("[", "")
                                  .replace("]", ""));
      }
   }
}

Output:-

[1, 2, 3, 4, 5]
1, 2, 3, 4, 5

In the replace() method of the Java string class, if we pass an empty string as the second parameter then it removes the first parameter sub-string from the given string.

In the previous Java print array without brackets program, we removed brackets from the given array. But if we also want to remove commas from the resultant string then we can use replace(“,”, ” “) and it will replace all commas from the given string with a space character.

Java print array without brackets and commas

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      int array[] = { 1, 2, 3, 4, 5 };
      for (int i = 0; i < 1; i++) {
         System.out.println(Arrays.toString(array));
         System.out.println(Arrays.toString(array)
                                  .replace("[", "")
                                  .replace("]", "")
                                  .replace(",", " "));
      }
   }
}

Output:

[1, 2, 3, 4, 5]
1 2 3 4 5

We can use simple for loop or for-each loop to display the array without using brackets.

Java print array without brackets using for loop

public class Main {
   public static void main(String args[]) {
      String array[] = { "Java", "Programming", "Language" };
      for (int i = 0; i < array.length; i++) {
         if(i == array.length - 1) {
            System.out.print(array[i] + " ");
         } else {
            System.out.print(array[i] + ", ");
         }
      }
   }
}

Output:-

Java, Programming, Language

Java print array without brackets and commas using for loop

public class Main {
   public static void main(String args[]) {
      String array[] = { "Java", "Programming", "Language" };
      for (int i = 0; i < array.length; i++) {
         System.out.print(array[i] + " ");
      }
   }
}

Output:-

Java Programming Language

Java print array without brackets and commas using for-each loop

public class Main {
   public static void main(String args[]) {
      String array[] = { "Java", "Programming", "Language" };
      for (String string : array) {
         System.out.print(string + " ");
      }
   }
}

Output:-

Java Programming Language

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *