wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to remove the [[ from arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to remove the [[ from arraylist" Watch "how to remove the [[ from arraylist" New topic
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: 40041
    
  28
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: 40041
    
  28
Override the toString() method. Remove the first and last characters from each element's printout.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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: 40041
    
  28
Using a regular expression might not help; the output might not always be numbers.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to remove the [[ from arraylist