6 Replies - 2055 Views - Last Post: 07 May 2011 - 08:26 AM Rate Topic: -----

#1 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

How to check if a string is the same to an element in an ArrayList?

Posted 06 May 2011 - 08:15 PM

Sorry for the multitude of questions!

I think I have one final question, and then I am done harrasing you guys for help!

Say I am given a string "x". How can i check if x is the same to any of my strings in my ArrayList? I looked around in the API, but I couldn't find anything.

This post has been edited by paki123: 06 May 2011 - 08:30 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How to check if a string is the same to an element in an ArrayList?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to check if a string is the same to an element in an ArrayList?

Posted 06 May 2011 - 08:59 PM

If you type into google "java 6 api" followed by the name of the class you're interested in, you will find a webpage giving all kinds of information about that class, including a list of all of that class' methods and explanations of what they do.

Try it; google "java 6 api string".
Was This Post Helpful? 0
  • +
  • -

#3 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: How to check if a string is the same to an element in an ArrayList?

Posted 06 May 2011 - 09:49 PM

Hmm not sure why you gave r.stiltskin a -1 rep for his perfectly valid and appropriate answer. Anyway, you said you looked at the API. Did you look at the documentation for the String class like r.stiltskin suggested? The String class has a method called "equals" which lets you test to see if one String is the same as another String.

Say you have a String object referenced by the variable called myString. If you want to test to see if myString is the same as, let's say, "java", you can use the equals method in the following manner:

if ( myString.equals( "java" ) )
{
   // do something
}



Thats pretty much straight out of the list of methods in the documentation for the String class. I realize it's daunting, but give what r.stiltskin suggested a go. Try to get more acquainted with the documentation and you can answer alot of your own questions and save yourself the time it takes to come here and type out the question. :)
Was This Post Helpful? 1
  • +
  • -

#4 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: How to check if a string is the same to an element in an ArrayList?

Posted 06 May 2011 - 10:19 PM

Iterator class can be used to iterate through the list and can be compared with your String
Was This Post Helpful? 0
  • +
  • -

#5 totomo999  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 05-May 11

Re: How to check if a string is the same to an element in an ArrayList?

Posted 07 May 2011 - 01:01 AM

You can use compareTo methode .This metode return 0 if the two strings are equal .
assume we have two strings s1 and s2 then you can know if they are the same or not by:
if(s1.compareTo(s2)==0){
//write what you want the programme to do if they are equal
}

Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to check if a string is the same to an element in an ArrayList?

Posted 07 May 2011 - 05:28 AM

View Postpaki123, on 06 May 2011 - 11:15 PM, said:

Say I am given a string "x". How can i check if x is the same to any of my strings in my ArrayList?

String abc = "x";
You want to know if the String in the ArrayList is also "x" ?
if(al.get(N).equals(abc)) {
You want to know if the String object in the ArrayList is abc ?
if(al.get(n) == abc) {
Was This Post Helpful? 1
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,918
  • Joined: 20-September 08

Re: How to check if a string is the same to an element in an ArrayList?

Posted 07 May 2011 - 08:26 AM

Quote

Say I am given a string "x". How can i check if x is the same to any of my strings in my ArrayList?


List<String> strings = ...;
int ixFound = strings.indexOf("x");
boolean found = (ixFound >= 0);

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1