Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Arraylist Issue

 
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 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.
 
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
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.
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are fields and methods in the String class which are specially designed to help with this problem.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Cupp wrote: . . . far more time consuming.

An extra ten minutes, and an extra 5 marks
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic