Solved

Searching about element in ArrayLists "JAVA"

Posted on 2006-07-11
8
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 12

Expert Comment

by:enachemc
ID: 17088360
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
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

by:riaancornelius
ID: 17088369
lol, I guess we agree on that then :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

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

Author Comment

by:javamate06
ID: 17089763
CEHJ

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

Expert Comment

by:enachemc
ID: 17089795
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:Mayank S
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

by:CEHJ
ID: 17090438
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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