• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist Issue  RSS feed

 
ms raaghu
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need help in understanding the below mentioned code:

i have a array-list like
ArrayList<String> arrList=new ArrayList<String>();
arrList.add("ABC");
arrList.add("Abc");
arrList.add("abc");
added three elements into this ..
but if i want to search without any case sensitiveness.. say
if i want true if arrList.contains("ABc") is executed but i am getting false because it is case sensitive ..
how can i achieve the same please suggest.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when you post source code.

How are you doing the searching in the ArrayList? Are you just using ArrayList.contains()? That will not do a case-insensitive comparison. You'll just have to iterate through the list yourself and check each element using a case-insensitive comparison to see if it is the element you're looking for.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are fields and methods in the String class which are specially designed to help with this problem.
 
Jason Cupp
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure there are better ways, but you could use a couple of temporary strings and just run the toLowerCase() or toUpperCase() methods on them to make them all the same case.
 
ms raaghu
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help, i just did a workaround , while adding into the arraylist i convertedinto lowercase so, the comparison became easy.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ms raaghu wrote:thanks for the help, . . . while adding into the arraylist i convertedinto lowercase . . .
You would have done better to go through the String class as I suggested; you would have found this and this. You could have written a wrapper class round your strings, and overridden its equals method to recognise ABC Abc and abc as equal, with one of those methods. That would have been a far more elegant solution.
 
Jason Cupp
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
ms raaghu wrote:thanks for the help, . . . while adding into the arraylist i convertedinto lowercase . . .
You would have done better to go through the String class as I suggested; you would have found this and this. You could have written a wrapper class round your strings, and overridden its equals method to recognise ABC Abc and abc as equal, with one of those methods. That would have been a far more elegant solution.
And far more time consuming.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Cupp wrote: . . . far more time consuming.
An extra ten minutes, and an extra 5 marks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!