I have two elements that I have in a List Object, but am trying to compare these elements and if they are the same return true otherwise false.
But am unsure how to get the two items from the list and compare?
Thanks,
I have two elements that I have in a List Object, but am trying to compare these elements and if they are the same return true otherwise false.
But am unsure how to get the two items from the list and compare?
Thanks,
Have you checked the API for List?
When you say the same, you mean the same Object (reference) or the Objects have the same value?
Have you tried looping the List?
Have you checked the API for List?
When you say the same, you mean the same Object (reference) or the Objects have the same value?
Have you tried looping the List?
Yes I have but I don't see any compare methods, I mean check the objects in the list and see if they have the same value.
No I haven't tried looping the list not familiar with this?? - there will only be two elements in the list to compare.
Thanks,
You mean you didn't notice methods like: get(int i) or size() ?
Do you know how to write a simple for loop?
for (int i=0;i<list.size();i++) {
Object obj = list.get(i);
}
Get the objects and compare them.
Ok, yes sorry I am a beginner but I see now - one more thing I have written my for loop, when comparing the two Strings, how do I call on each one? (like differentiate one from the other?)
Thanks,
Let's see some code first because your questions doesn't make any sense
for (int i=0;i<areTheyRelated.size();i++) {
String s = areTheyRelated.get(i);
}
So this will return two Strings from my list yes. When the two strings are returned I am not sure how I should reference them to to say e.g. if(String 1 == String 2) return true etc..
> If you are sure you will have only 2 Strings then you don't need the List.
> If you need to compare 2 Objects first you will need to get them into separate variables. You have to be more specific on what you want because, You will need to have at least one of them and then loop the list to find the other.
> Definitely for Strings and usually for other Objects use the equals() method.
This:
if (String1 == String2) checks if they are the SAME Object.
This:
if ( String1.equals(String2) ) checks their values.
Use the equals()
Ok, I get it that way.
The object I am getting the strings from is a list. So I go through that object in the for loop and get the two strings but how do I make it so that the two strings can be referenced to like you reference them i your last post? i.e assign them names of string1 and string2?
Yes
sorry, I was asking "how do I make it so that the two strings can be referenced to like you reference them i your last post? i.e assign them names of string1 and string2?"
You need to get the values from the List, put them into 2 different variables and compare them.
But you can compare them directly from the List as well:
if ( list.get(i).equals(list.get(j)) )
Remember the list.get(i) as a whole is a String.
I suggest you explain your entire problem because your questions had me confused regarding what you are trying to achieve, because I believe there might be a different way to do what you want.
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
PS: This has nothing to do with this post or the poster. It is for whoever is watching.:
1) I don't answer PMs with stupid questions
2) If someone wants to say something just send me a PM with what they want.
3) Me being online or not has nothing to do with my ability to receive PMs. If someone sends something I will see it the next time I login
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.