➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
How to Print Arraylist Without Brackets Java | In this case, we print the array list without brackets, usually when we use ArrayList the elements are printed within the brackets. To do this we can use replace method which replaces the brackets by space. Now see some below examples to get to the know the problem clearly,
1) ArrayList = [1,2,3,4,5]
Result = 1, 2, 3, 4, 5
2) ArrayList = [“abc”, “bcf”, “fgf”]
Result = abc, bcf, fgf
3) Arraylist = [123478, 145678, 2344, 12345]
Result = 123478, 145678, 2344, 12345
4) ArrayList = [1.44, 3.55, 2.33]
Result = 1.44, 3.55, 2.33
We will demonstrate this problem by using different data types.
How To Print Arraylist Without Brackets Java – ArrayList of Numbers
import java.util.ArrayList;
public class Main {
public static void main(String args[]) {
ArrayList<Integer> list = new ArrayList<>();
list.add(4);
list.add(5);
list.add(434);
list.add(6);
System.out.println(list.toString()
.replace("[", "")
.replace("]", ""));
}
}
Output:
4, 5, 434, 6
We have created a list of integer values and added some elements to that list. After that, we converted the ArrayList to a string. It gives result with brackets like [4, 5, 434, 6].
From this we want to remove square brackets ” [ ” and ” ] “. For this purpose, we used replace() method of the Java string class. If we pass an empty string (“”) as the second parameter to the replace() method then it removes the first parameter from the string. Let us see some more examples to learn how to print ArrayList without brackets Java.
Print Arraylist Without Brackets Java – ArrayList of String
import java.util.ArrayList;
public class Main {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Programming");
list.add("Language");
System.out.println(list.toString()
.replace("[", "")
.replace("]", ""));
}
}
Output:-
Java, Programming, Language
Print Arraylist Without Brackets Java – ArrayList of Double
import java.util.ArrayList;
class Main {
public static void main(String args[]) {
ArrayList<Double> list = new ArrayList<>();
list.add(3.44);
list.add(4.567);
list.add(78.236);
System.out.println(list.toString()
.replace("[", "")
.replace("]", ""));
}
}
Output:
3.44, 4.567, 78.236
Print ArrayList Java Without brackets – ArrayList of Characters
import java.util.ArrayList;
public class Main {
public static void main(String args[]) {
ArrayList<Character> array = new ArrayList<>();
array.add('b');
array.add('f');
array.add('f');
array.add('d');
array.add('s');
System.out.println(array.toString()
.replace("[", "")
.replace("]", ""));
}
}
Output:
b, f, f, d, s
Also see:- Java Collection Framework Overview
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!