# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
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.
Print an Array Without Commas And Brackets In Java
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
Print an Array Without Brackets Using For Loop
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!