• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing brackets from arraylist printout.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4908
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way is to roll your own method to print the array:

System.out.println(listToString(arraylist));
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 488
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Brian,

Excellent help from everyone, I appreciate it!
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...:-)
 
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.Arrays;
import java.util.List;
 
public class StingArrayToArray {
 
    public static void main(String[] args) {
        String[] strArray = { "Satya", "Ronith", "Swamy" };
 
        // convert Array to List
        List<String> list = Arrays.asList(strArray);
 
        String output = list.toString().replaceAll("\\[|\\]", "");
        System.out.println(output);
    }
 
}
 
Mike Simmons
Master Rancher
Posts: 4908
74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as we're revisiting this post from 2009, I would now use

That method came out in 2014, so wasn't available when I answered back in 2009.
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are all sorts of ways you could have done it before 2014. I would, like you, have used a StringBuilder. Iterate the List, append the element and then append ", " or similar. Then reduce the length of the StringBuilder by the length of the delimiter (in this case two characters) to remove the ", " from its end.
But I agree that String#join() is a very elegant and concise way to achieve the same.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic