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 7415807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:22:18+00:00 2026-05-29T07:22:18+00:00

I am trying to Add and delete from an arraylist of a collection of

  • 0

I am trying to Add and delete from an arraylist of a collection of objects in the same loop but in different methods and my problem is after adding and deleteing i try to print my array list but i cant get the values that is added recently. I am using the ListIterator to add and the Iterator to delete because I read in a bunch of website that this is the only ways to add and delete and the change will take place directly in the arraylist but my problem is that it is adding and deleting and the arraylist count or reference number on the debugger looks fine but when I search through it I cant find the new. added object and I still find the deleted objects.
this is the remove and add methods

public void RemoveFromAnimalList(Animal A) {
    Iterator<Animal> i = AnimalList.iterator();
    while (i.hasNext()) {
        if (A == i.next()) {
            i.remove();
        }
    }
}   

public boolean AddNewAnimalList(Animal A) {
    ListIterator litr = AnimalList.listIterator();
    Animal B = null;
    while (litr.hasNext()) {
        B = (Animal) litr.next();

            litr.add(A);
            return true;

    }
    return false;
}

this is the method that is calling those methods above and i have tried everything and debug everything and everytime i cant find the new add element eventhough the arraylist number looks right i have 400 objects and when i delete it reachs to 399 and then again when i add it get back to 400 but when i try to print and go through the objects i cant find the new ones.

public void FirstBirth() {
    TheXYAxisControl XYSave = null;
    int X = 0;
    int Y = 0;
    for (Animal A : AnimalList) {
        if (A.Type.equals("Empty")) {

            X = A.XAxis;
            Y = A.YAxis;
            XYSave = new TheXYAxisControl(X, Y);
            TheXYaxisArrayList.add(XYSave);

        }
    }
    Random RandomNumberGen = new Random();

    for (int s = 0; s < 3; s++) {

        int RandomNumberGenerator = RandomNumberGen.nextInt(TheXYaxisArrayList.size());

        Animal NewAnimal = new Animal(0, "Male", 0, TheXYaxisArrayList.get(RandomNumberGenerator).getX(), TheXYaxisArrayList.get(RandomNumberGenerator).getY(), 0);
        RemoveFromAnimalList(AnimalList.get(RandomNumberGenerator));
        AddNewAnimalList(NewAnimal);
        RemoveFromOtherList(TheXYaxisArrayList.get(RandomNumberGenerator));
    }

    int RandomNumberGenerator = RandomNumberGen.nextInt(TheXYaxisArrayList.size());
    Animal NewAnimal = new Animal(0, "Female", 0,  TheXYaxisArrayList.get(RandomNumberGenerator).getX(),    TheXYaxisArrayList.get(RandomNumberGenerator).getY(), 0);
    RemoveFromAnimalList(AnimalList.get(RandomNumberGenerator));
    AddNewAnimalList(NewAnimal);
    RemoveFromOtherList(TheXYaxisArrayList.get(RandomNumberGenerator));

}

please if you have any question or other part of code i can explain.

  • 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-29T07:22:20+00:00Added an answer on May 29, 2026 at 7:22 am

    You should really use Java generics in your Collection, List, Iterator, ListIterator etc and avoid casting.

    Also you’re comparing with == sign here:

    if (A == i.next())
    

    Which is just comparing 2 references. Change that to:

    if (A.equals(i.next()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i`m trying to make an NSMutableArray From NSUserDefaults so i can add/delete and edit
When I'm trying to delete an external event from my calendar, if I add
I have a problem when trying to delete rows from my UITableView: The UITableView
i am trying to add and delete records(games) from my database using jquery and
I am trying to add a Delete button to my product-page, but I am
Trying to Delete row from sql database. Quotation mark is the problem I tried
Am trying to add a link besides the Edit | Delete links in wordpress
I have been trying to add an askquestion dialog box to a delete button
I am trying to manipulate SharePoint's default calendar to add, edit and delete items
Im new to asp.net mvc. I'm trying add new model class but it got

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.