| Author |
How do I remove the brackets from the ArrayList
|
Arun S. Nair
Greenhorn
Joined: Jul 22, 2012
Posts: 4
|
|
How do I remove the brackets from an ArrayList. This is my code. I need a loop that returns a string for my getBuiltInDevices. This code shown below is from one of my subclasses. I was supposed to add builtInDevices, which I did in my stockMain class. When I printed out the output I got [BulletScan M80 Sheetfed Scanner, Lenovo Wireless Mouse N3901A, Lenovo Headset P950]. How do I remove the brackets.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
What brackets in an ArrayList? Do you mean the square brackets which enclose the list of elements when you call toString()?
Override toString and return a different format. Create a utility class with a listToString(List<?>) method.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
And welcome to the Ranch
I shall edit your post in line with this link and you can see how better it looks.
|
 |
Arun S. Nair
Greenhorn
Joined: Jul 22, 2012
Posts: 4
|
|
Yes, I mean the brackets which enclose the list of the elements. I have tried to do a for loop but I am not able to solve it. What do i put in the parameter of add. I alreaady have a toString method.
String s="";
for(int i = 0;builtInDevices.get(i) !=null && i<builtInDevices.size();i++)
{
if(builtInDevices.get(i+1)==null)
{
s=s+builtInDevices.add();
}
else
{
s=s+builtInDevices.add() + ", ";
}
}
return builtInDevices;
}
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Yesterday, I wrote: . . . I shall edit your post in line with this link and you can see how better it looks.
Now it is your turn to use what it says in that link.
You need to tell us more details. How did you use the for loop? What do you mean about parameters to add()? I don’t think that will affect your output. We can’t help if we don’t know what you did.
|
 |
 |
|
|
subject: How do I remove the brackets from the ArrayList
|
|
|