Solved

Two ArrayList equalty index.

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MaxSpan challenge 9 65
c# combobox autocomplete behavior 6 92
Path to Python 9 39
Adoquery sql  left join does not work 25 44
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

707 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

14 Experts available now in Live!

Get 1:1 Help Now