Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Two ArrayList equalty index.

Posted on 2006-07-11
2
Medium Priority
?
329 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 …

927 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