Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5933031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:55:01+00:00 2026-05-22T14:55:01+00:00

I have an arrayList called A, say, which is non-empty. I create another arrayList,

  • 0

I have an arrayList called A, say, which is non-empty.

I create another arrayList, B, and put in some elements of A.

for(someobject obj : A){
                   if(some_condition_is_met)
                             B.add(obj);
}

Then I do some stuff with B (never modifying, shuffling, or deleting objects from it. I pick an element of B, say B.get(i).

Now, I want to find B.get(i)’s position in A.

I tried

for(someobject obj : A){
                   if(obj.equals(B.get(i))
                             return A.lastIndexOf(obj);
}
return null;

But this kept returning null. That is, “equals” doesn’t match the objects.

SO in my frustration I tried:

return A.get(A.lastIndexOf(B.get(i));

which caused an ArrayINdexOutOfBounds exception.

Any ideas what I should do? I’ve got a feeling I’m missing something obvious.
One more point – this is an incredibly simplified explanation of what I’m doing. In the above example creating B might seem pointless, but it is necessary.

ANSWERS TO QUESTIONS:

1)The objects are custom objects. Ah,… I didn’t override equals. That might be it.

2) I can be sure any object in B mst B in A because on creating B, first I remove all from it, just in case, then I add only object from A and I do not tamper with the objects once in B.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:55:01+00:00Added an answer on May 22, 2026 at 2:55 pm

    The principle of what you are trying to do should work. I could make the quibble that there is little point in doing the FOR loop to search through A to find an instance of the object from B, and then doing a lastIndexOf on it. lastIndexOf will iterate through A anyway. You are searching for something, and then when you find it you are just leaving it where it lies and searching for it all over again. (Admittedly, your first search is from the beginning of A while lastIndexOf will search from the end. So this is like looking for your car keys starting in the basement and working up to the attic. Then when you find them, you leave them there, and then go to the attic and start the search again working your way down to the basement.)

    I suspect that the problem is not in the concept, but in the details. You might show the actual code. If the code is too complex, try simplifying it down to the essentials and see if the error still occurs.

    One possibility: Do you really add the object from A to B directly? Or are you creating a new object for B that is a copy of the object from A? If the latter, then unless you override equals() it’s not going to work, because the default equals() for a user-defined object just checks if they are the same instance, not “equivalent” objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have already created an iterator called iter and an arraylist called database.
I have a driver class called Advantech which is stored inside an ArrayList .
I have a java application which uses JPA. Say I have an entity called
So let's say I have an ArrayList of Strings, called list1, containing the strings,
I have an ArrayList called account which contains Strings. I'm trying to write a
Basically, I have a Container class called Employees which has in it an ArrayList.
I have an arraylist that contains items called Room. Each Room has a roomtype
I have a ArrayList made up of different elements imported from a db, made
I have an arraylist which has a class datastructure. public class empRec { public
I have an arraylist (eg: h1) contain some urls. eg: http://www.google.com,http://www.yahoo.com,http://www.xyz.com ,..... How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.