Solved

Two ArrayList equalty index.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 87
detecting  the added row index in a datagridview 3 54
Path to Python 9 104
egit plugin on eclipse 8 28
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…
This is an explanation of a simple data model to help parse a JSON feed
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 …
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 …

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now