Solved

Searching about element in ArrayLists "JAVA"

Posted on 2006-07-11
8
233 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 Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
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…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

688 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