javamate06
asked on
Searching about element in ArrayLists "JAVA"
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( yourArrayL ist.get(k) ))
return true;
else
return false;
}
}
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(
return true;
else
return false;
}
}
public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
for(int k = 0;k <= yourArrayList.size();k++)
{
if( myArrayList.get(0).equals( yourArrayL ist.get(k) ))
return true;
}
return false;
}
{
for(int k = 0;k <= yourArrayList.size();k++)
{
if( myArrayList.get(0).equals(
return true;
}
return false;
}
lol, I guess we agree on that then :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CEHJ
in your solution, when it will return false?
in your solution, when it will return false?
it will return false if that element is not found in yourArrayList (same as your implementation, but already made by Java).
In fact, instead of calling isThere (), I would directly use CEHJ's code wherever the method was supposed to be called....
:-)
public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
for(int k = 0;k <= yourArrayList.size();k++)
{
if( myArrayList.get(0).equals(
return true;
}
return false;
}