Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Two ArrayList equalty index.

Posted on 2006-07-11
2
Medium Priority
?
326 Views
Last Modified: 2010-04-17
I have these two arrayLists

ArrayList myArrayList = new ArrayList();
ArrayList yourArrayList = new ArrayList();


I want a method isEqualTo() to test if first element in myArrayList is equal to the last element in yourArrayList, if it is equal return true and false otherwise .

I tried this code but it was not work:

if(!myArrayList.isEmpty())
            {
                 for(int k = yourArrayList.size() ; k < yourArrayList.size() ; k--)    
              {
                if(yourArrayList.get(k).equals(myArrayList.get(1)))
                   return true;      /*System.out.println("\nFound\n");  */
              }
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 Comments
 
LVL 9

Expert Comment

by:DrAske
ID: 17079910
I don't know why you're using a for loop .. ??
as you mentioned you want to compare the first element in *myArrayList* with the last element in *yourArrayList* ..
so do it simply :
{
 ArrayList myArrayList = new ArrayList();
 ArrayList yourArrayList = new ArrayList();

// Insert element to ArrayLists

      if(myArrayList.Count != 0 && yourArrayList.Count != 0)
      {
            int LastElement = yourArrayList.Count-1;
            
                                if(yourArrayList[LastElement].Equals(myArrayList[0]))
                                           return true;
      }
return false;
}

regards, Ahmad;
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 200 total points
ID: 17079915
Your code checks to find the last element in yourArrayList that matches the second element in myArrayList. To see if the last element in yourArrayList is the same as the first in myArrayList, you can put it all in one line if you like, like this:

   return ((!myArrayList.isEmpty()) && yourArrayList.get(yourArrayList.size()-1).equals(myArrayList.get(0)));

If you want to retail legibility, you can do it like this:

if (!myArrayList.isEmpty())
{
     if (yourArrayList.get(yourArrayList.size()-1).equals(myArrayList.get(0)))
          return true;     /*System.out.println("\nFound\n");  */
}
return false;  /* If the array is empty, it should be false too! */

I'm assuming that this is Java or some other language that bases indices from zero. If the index is one-based, the comparison should be:

     if (yourArrayList.get(yourArrayList.size()).equals(myArrayList.get(1)))
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

715 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