Solved

# Searching about element in ArrayLists "JAVA"

Posted on 2006-07-11
Medium Priority
239 Views
If I have two arrayLists, and I want to search if first element in myArrayList is there in yourArrayList, if it is found return true and false otherwise;
I have problem in my code which looping in returning values. So, if it the element not found then it will looping returning false, and I want only one return value, either true or false.

public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
for(int k = 0;k <= yourArrayList.size();k++)
{
if( myArrayList.get(0).equals(yourArrayList.get(k)))
return true;

else
return false;
}

}
0
Question by:javamate06
• 2
• 2
• 2
• +2

LVL 12

Expert Comment

ID: 17088360

public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
for(int k = 0;k <= yourArrayList.size();k++)
{
if( myArrayList.get(0).equals(yourArrayList.get(k)))
return true;
}
return false;
}
0

LVL 9

Expert Comment

ID: 17088363
public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
for(int k = 0;k <= yourArrayList.size();k++)
{
if( myArrayList.get(0).equals(yourArrayList.get(k)))
return true;
}
return false;
}
0

LVL 9

Expert Comment

ID: 17088369
lol, I guess we agree on that then :)
0

LVL 86

Accepted Solution

CEHJ earned 150 total points
ID: 17088663
public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
return yourArrayList.contains(myArrayList.get(0));
}
0

Author Comment

ID: 17089763
CEHJ

in your solution, when it will return false?
0

LVL 12

Expert Comment

ID: 17089795
0

LVL 30

Expert Comment

ID: 17089811
In fact, instead of calling isThere (), I would directly use CEHJ's code wherever the method was supposed to be called....
0

LVL 86

Expert Comment

ID: 17090438
:-)
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.