[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Searching about element in ArrayLists "JAVA"

Posted on 2006-07-11
8
Medium Priority
?
235 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Accepted Solution

by:
CEHJ earned 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.
Suggested Courses

650 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