| Author |
how to remove the [[ from arraylist
|
sahana mithra
Ranch Hand
Joined: Oct 26, 2010
Posts: 72
|
|
I have a value from array list. [[2, 3, 4]]
I just want to remove [[ and ]]. My output should be 2,3,4
I tried pattern.compile([\\[[]);
But i m not able to do it.
Please help me to do this.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
How do you get the square brackets? Is it from the toString() method? Have you read its documentation? Why are you getting [[...]] rather than [...]?
Why don't you override that method to print without square brackets?
|
 |
sahana mithra
Ranch Hand
Joined: Oct 26, 2010
Posts: 72
|
|
Hi,
I found the reason. I am getting values from a method which returns as an arrayList and I am storing that in a list again. Here is my code
Help me to solve this problem. How to remove that [[]]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
Override the toString() method. Remove the first and last characters from each element's printout.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
Personally, I would recommend not using the toString() method. Use an iterator, a string builder, and create the string that you want -- in the exact format that you want it.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
jishnu dasgupta
Ranch Hand
Joined: Mar 11, 2011
Posts: 103
|
|
|
You might also modify the regex to make it as....\d(,\d)*.....if the pattern is always in the form (number,number,number)....Keeping it greedy will hopefully extract the entire number set and neglect anything else.
|
If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
Using a regular expression might not help; the output might not always be numbers.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
So, you have a List inside a List? Then it's perfectly normal that you get it between [[ and ]].
The first [ ] are for the outer list, that contains one element, which is the inner list. The second [ ] are for the inner list.
Why are you storing a list inside a list?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: how to remove the [[ from arraylist
|
|
|