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