File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Removing brackets from arraylist printout. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing brackets from arraylist printout." Watch "Removing brackets from arraylist printout." New topic
Author

Removing brackets from arraylist printout.

Scotty Young
Greenhorn

Joined: Oct 26, 2006
Posts: 10
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Jan 17, 2006
Posts: 1296
The best way is to roll your own method to print the array:

System.out.println(listToString(arraylist));


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2597
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Not really.
Your code will print all the values vertically.

It should be modified to


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Scotty Young
Greenhorn

Joined: Oct 26, 2006
Posts: 10
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
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2597
    
    9

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

Joined: Oct 26, 2006
Posts: 10
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

Joined: Oct 26, 2006
Posts: 10
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

Joined: Nov 07, 2008
Posts: 488
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.

SCJA
~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I was too slow! Grats on getting it working.
Scotty Young
Greenhorn

Joined: Oct 26, 2006
Posts: 10
Cheers Brian,

Excellent help from everyone, I appreciate it!
ali akhtar
Greenhorn

Joined: Dec 26, 2009
Posts: 1
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

Joined: Aug 22, 2014
Posts: 206

This post just helped me out five years after it was posted - thanks to K.Tsang and to Scott for asking!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing brackets from arraylist printout.