In this tutorial, We will learn how to print Array without brackets in Java. We will accept any Array from the end-user and will display the elements of the Array without brackets.
What is an Array in Java?
An Array is a collection object which contains multiple elements of the same type. It provides contiguous memory allocation and fixed size of elements. It is also based on indexing which starts from “0”.
Let’s see the below example of an Array with a total of 8 elements where the first index starts from “0” and the last index is “7”. We have differentiated indexes as indices and values as elements. The length of the below Array is 8.
Example of String Array
String[] str = {"Let's", "learn", "Array", "in", "Java"};
Code language: JavaScript (javascript)
Example of Integer Array
int[] num = {45, 25, 65, 8, 47, 5};
Read Also: Java Program To Remove Duplicate Words In A String
Why do We Need to Print Array Without Brackets in Java?
In most situations, we just need to print the desired Array as it is given, but sometimes we may need to display the elements of an Array with some modification.
Let’s try to understand the reason why do we need to print an Array without brackets through an example.
Suppose we are taking the example of a Dice of LUDO game. We are preparing a game application where we will print the digits of each side of a Dice one by one. Suppose we have stored the digits of each side in an Array as given below.
int[] dice = {1, 2, 3, 4, 5, 6};
So through our application, we will display the digits of a dice. So here we need to display the elements of the above Array without brackets and we can format the output as we want to display it to end-users also.
Similarly in real-world projects also we may need to display the elements of the Array without brackets. So let’s see how we can print an Array without brackets in Java.
Here we have given a small example of how we can print an Array without brackets. For detailed knowledge, please refer to other methods as well as those given just below this example.
How to Print Array Without Brackets in Java?
We can print an Array without brackets in Java by using the following methods
- Print Array without brackets using for loop
- Print Array without brackets using StringBuffer class
- Print Array without brackets using join( ) function
- Print Array without brackets using the toString( ) method
1. Print Array without brackets using for loop
We can print an Array without brackets by using a simple for loop in Java. Let’s see the complete program below.
/**
* Java program to print Array without brackets
*/
package programs;
public class PrintArrayWithoutBracketsUsingForLoop {
// Method to print given Array without brackets
static void printArray() {
// Declare an Array
String[] str = { "Let's", "learn", "Array", "in", "Java" };
// Print Elements of given Array using for loop
for (int i = 0; i < str.length; i++) {
System.out.print(str[i] + " ");
}
}
public static void main(String[] args) {
// Call printArray() function to print
printArray();
}
}
Code language: JavaScript (javascript)
Output
Let's learn Array in Java
2. Print Array without brackets using StringBuffer class
We can print an Array without brackets by using the StringBuffer class in Java. Let’s see the complete program below.
/**
* Java program to print Array without brackets using StringBuilder class
*/
package programs;
public class PrintArrayWithoutBracketsUsingStringBuffer {
// Method to print given Array without brackets
static void printArray() {
// Declare an Array
String[] str = { "Let's", "learn", "Array", "in", "Java" };
StringBuffer sb = new StringBuffer();
// Format given Array with elements without brackets
for (int i = 0; i < str.length; i++) {
sb.append(str[i]).append(" ");
}
// Convert StringBuffer to String and display to user
System.out.println(sb.toString());
}
public static void main(String[] args) {
// Call printArray() function to print
printArray();
}
}
Code language: JavaScript (javascript)
Output
Let's learn Array in Java
3. Print Array without brackets using join( ) function
We can print an Array without brackets by using the predefined join() function in Java. Let’s see the complete program below.
/**
* Java program to print Array without brackets using join( ) function
*/
package programs;
public class PrintArrayWithoutBracketsUsingForLoop2 {
// Method to print given Array without brackets
static void printArray() {
// Declare an Array
String[] str = { "Let's", "learn", "Array", "in", "Java" };
// Convert given Array to a String by using join() function
String joined = String.join(" ", str);
// Display converted String
System.out.println(joined);
}
public static void main(String[] args) {
// Call printArray() function to print
printArray();
}
}
Code language: JavaScript (javascript)
Output
Let's learn Array in Java
4. Print Array without brackets using the toString( ) method
We can print an Array without brackets by using the predefined toString() method in Java. Let’s see the complete program below.
/**
* Java program to print Array without brackets using toString( ) method
*/
package programs;
import java.util.Arrays;
public class PrintArrayWithoutBracketsUsingForLoop2 {
// Method to print given Array without brackets
static void printArray() {
// Declare an Array
String[] str = { "Let's", "learn", "Array", "in", "Java" };
// Convert given Array to a String by using toString() and replace() methods
String s = Arrays.toString(str).replace("[", "").replace("]", " ").replace(",", "");
// Display converted String
System.out.println(s);
}
public static void main(String[] args) {
// Call printArray() function to print
printArray();
}
}
Code language: JavaScript (javascript)
Output
Let's learn Array in Java
We hope We have resolved your issue or helped you to find the solution which you are looking for. Through this tutorial, if you have learned something or fulfilled your requirement to perform your program, then please share this tutorial with your friends, and please go through our other popular tutorials also.
Recommended Articles
- Java Program To Print Vowels In A String
- Java Program To Remove Duplicate Words In A String
- Best 2 Ways to Give Tab Space in Java
- How to convert List to Byte Array in Java?
- How To Check String Contains Special Characters In Java?
FAQs
Can we print an array in Java without a loop?
Yes, We can print an Array in Java without a loop by using toString( ), and join( ) methods. We can also use StringBuffer or StringBuilder class.
When do we need to print Array without brackets in Java?
In real-time projects, we may need to display the elements of an array without brackets to format as per our requirements. So we can do that by using the above methods.
What are indices in an Array in Java?
The index of elements is also known as indices which start with “0” and increase by “1”.
2 thoughts on “4 Best Ways to Print Array Without Brackets in Java”