• 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

how to remove the [[ from arraylist

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Override the toString() method. Remove the first and last characters from each element's printout.
 
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
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
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a regular expression might not help; the output might not always be numbers.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic