Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to remove the [[ from arraylist

 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override the toString() method. Remove the first and last characters from each element's printout.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a regular expression might not help; the output might not always be numbers.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic