Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing brackets from arraylist printout.

 
Scotty Young
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

When you "system.out.print(arraylist)" an arraylist, it will give you something like [item1, item2].

Im wondering how I can remove the "[" and "]" brackets fromt he printout, or even if i pass it in as another variable.

Thanks,

Scott.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't really change the behavior of ArrayList's toString() method (which was called implicitly by System.out when you passed an Object as a parameter). You basically have two options: either (1) use a different method (your own) to make a String out of the ArrayList , or (2) remove the braces from the String after toString() was called. For the first, you can create a StringBuilder, then simply loop through the list and addeach element to the StringBuilder, separated by commas. Or for the second, String's substring() method may be useful.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way is to roll your own method to print the array:

System.out.println(listToString(arraylist));
 
K. Tsang
Bartender
Posts: 3502
14
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use the enhanced-for loop. Suppose your ArrayList is declared as new ArrayList<String>(); then you can do this:


If you are not familiar with generics or what the <String> means, it's really the type of objects in the array list. ArrayList<String> means a array list containing strings. ArrayList<ABC> means a array list containing ABC object.
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really.
Your code will print all the values vertically.

It should be modified to

 
Scotty Young
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woah, thanks for the replys everyone.

I want instead of doing:



I want to take String s, and pass it into another method, e.g.:



However I can't access s outside the loop.
 
Henry Wong
author
Marshal
Pie
Posts: 21407
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However I can't access s outside the loop.


That is because the variable is out of scope. Your options are (1) change the declaration location of "s" so that it is in scope, or (2) assign the value of s (in the loop) to some variable that is in scope.

Henry
 
K. Tsang
Bartender
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it depends on what you want to do in your method. If it's just output call it inside the loop. If it must be outside, pass in the entire arrayList. But then in the method you may need to get the correct index/value.
 
Scotty Young
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, what I want to do is, have all the strings in the ArrayList<String> assigned to a single string. I can then pass this string into a method.

The aim is to build a php url request like so
 
Scotty Young
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the advice everyone, what I finally did was this:



Basically it just gets all the objects form the arraylist, sets them as Strings. Then it adds all this strings to a string builder, and creates a new string using the string builder. So all my strings are now one single string object.

W00t!
 
Brian Legg
Ranch Hand
Posts: 488
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just do what you are doing already but trim the first and last characters (the "[]") off using the substring method. Just do a search for substring or string manipulation and you'll find pleanty of examples.
 
Brian Legg
Ranch Hand
Posts: 488
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was too slow! Grats on getting it working.
 
Scotty Young
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Brian,

Excellent help from everyone, I appreciate it!
 
ali akhtar
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Young wrote:Hi all,

When you "system.out.print(arraylist)" an arraylist, it will give you something like [item1, item2].

Im wondering how I can remove the "[" and "]" brackets fromt he printout, or even if i pass it in as another variable.

Thanks,

Scott.


Hi Scotty,

the most simple solution for removing the brackets is,

1.convert the arraylist into string with .toString() method.

2.use String.substring(1,strLen-1).(where strLen is the length of string after conversion from arraylist).

3.Hurraaah..the result string is your string with removed brackets.

hope this is useful...:-)
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This post just helped me out five years after it was posted - thanks to K.Tsang and to Scott for asking!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic