Solved

Searching about element in ArrayLists "JAVA"

Posted on 2006-07-11
8
231 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
login jsp example 24 65
tomcat administrtor 12 61
ejb message driven bean mdb creation steps 2 19
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 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