Solved

Searching about element in ArrayLists "JAVA"

Posted on 2006-07-11
8
226 Views
Last Modified: 2010-03-31
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
Comment
Question by:javamate06
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 12

Expert Comment

by:enachemc
Comment Utility
Use the following code instead.

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

by:riaancornelius
Comment Utility
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

by:riaancornelius
Comment Utility
lol, I guess we agree on that then :)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
Comment Utility
public static boolean isThere(ArrayList myArrayList,ArrayList yourArrayList)
{
    return yourArrayList.contains(myArrayList.get(0));
}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:javamate06
Comment Utility
CEHJ

in your solution, when it will return false?
0
 
LVL 12

Expert Comment

by:enachemc
Comment Utility
it will return false if that element is not found in yourArrayList (same as your implementation, but already made by Java).
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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

by:CEHJ
Comment Utility
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now