Solved

Two ArrayList equalty index.

Posted on 2006-07-11
2
300 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
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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from 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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

786 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