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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:14:40+00:00 2026-06-18T01:14:40+00:00

So I have an ArrayList of objects of my own class: public class A

  • 0

So I have an ArrayList of objects of my own class:

public class A
{...}

Now say I have a variable that holds one of these objects from the list. Now I know if I change that object (e.g. set a field on that object), that this changes the memory that holds the object itsself, so both references (the variable and the internal array variable that holds the object in the list) are still identical and both still show to the same, now modified, object. Normal behavior!

But I also want that to be the case when I replace the object in the list that my variable points to. That is when I do

myList.set(theObjectPosition, newInstanceOfA);

…in this case I also want my variable to automagically have the reference to this new instance. What would be the easiest way to implement such behavior?

This is why I need this:

In short, I have a set of classes that handles undos/redos of changes made to a ArrayList (which I have not extended, that would not solve the problem anyway?). The classes are called

UndoAction, Add, Remove, Modify (the latter 3 inherit from UndoAction)

All classes have methods undo() and redo().

Example by describing (code is at the end if needed):

Say I want to add an item to a List: I do new Add(myObject), then I want to modify the item in the list so I do new Modify(myObject), after which it creates a DeepCopy of the object state as backup, after which I THEN change the object itsself (after having done the call to new Modify(..)). Then I do undo() on the Modify object so it does list.set(somePos, previousDeepCopy) (hence this post, the deepcopy made, by nature, is a new instance which messes things up!!!)….

So you can imagine that this list.set poses some problems. Any reference to the replaced object will be gone. So I can’t work effectively with the list like that if it always replaces references to objects like that, hence my undo manager is doomed to failure this way.

So how do I combat these design flaws? Any suggestions are welcome.


Some code:

 protected abstract class UndoAction {
    protected HashSet<Integer> ids = new HashSet<Integer>();
    protected Marker marker;

    public UndoAction(List<E> l) {
      for (E e : l) {
        if (!entities.contains(e)) {
          entities.add(e);
          trace.append(entities.indexOf(e), new Stack<UndoAction>());
        }
        ids.add(entities.indexOf(e));
      }
    }

    public boolean sameAffectedTargets(UndoAction undoAction) {
      if (this.ids.containsAll(undoAction.ids) && undoAction.ids.containsAll(this.ids))
        return true;
      return false;
    }

    public Marker getMarker() {
      return new Marker(this);
    }

    public void firstRun() {
    }

    public void undo() {
    }

    public void redo() {
    }

    protected List<Data> getCopyEntities() {
      List<Data> l = new ArrayList<Data>();
      for (Integer id : ids) {
        E e = entities.get(id);
        int pos = adapterList.indexOf(e);
        l.add(new Data(id, pos, e));
      }
      return l;
    }

    protected List<Data> getDeepCopyEntities() {
      List<Data> l = new ArrayList<Data>();
      for (Integer id : ids) {
        E e = DeepCopy.copy(entities.get(id));
        int pos = adapterList.indexOf(entities.get(id));
        l.add(new Data(id, pos, e));
      }
      return l;
    }

    public void addEntities(List<Data> datas) {
      for (Data d : datas)
        d.addEntity();
    }

    public void setEntities(List<Data> datas) {
      for (Data d : datas)
        d.setEntity();
    }

    public void removeEntities(List<Data> datas) {
      for (Data d : datas)
        d.removeEntity();
    }

    protected class Data {
      public int id;
      public int pos;
      public E entity;

      public void addEntity() {
        entities.set(this.id, this.entity);
        adapterList.add(this.pos, this.entity);
      }

      public void setEntity() {
        entities.set(this.id, this.entity);
        E oldEntity = adapterList.get(this.pos);
        adapterList.set(this.pos, this.entity);
        notifyEntityReplaced(oldEntity, adapterList.get(this.pos), this.pos);
      }

      public void removeEntity() {
        entities.set(this.id, null);
        adapterList.remove(this.entity);
      }

      public Data(int id, int pos, E entity) {
        this.id = id;
        this.pos = pos;
        this.entity = entity;
      }
    }
  }

  protected class Add extends UndoAction {
    protected List<Data> addBackup;

    public Add(List<E> l) {
      super(l);
    }

    @Override
    public void undo() {
      super.undo();
      addBackup = getCopyEntities();
      removeEntities(addBackup);
    }

    @Override
    public void firstRun() {
      super.firstRun();
      adapterList.addAll(entities);
    }

    @Override
    public void redo() {
      super.redo();
      addEntities(addBackup);
    }
  }

  // call before modifying
  protected class Modify extends UndoAction {
    protected List<Data> beforeDeepCopies;
    protected List<Data> afterDeepCopies;

    public Modify(List<E> l) {
      super(l);
    }

    @Override
    public void undo() {
      super.undo();
      if (!skipModifying) {
        if (afterDeepCopies == null)
          afterDeepCopies = getDeepCopyEntities();
        setEntities(beforeDeepCopies);
      }
    }

    @Override
    public void firstRun() {
      super.firstRun();
      if (!skipModifying) // TODO
        beforeDeepCopies = getDeepCopyEntities();
    }

    @Override
    public void redo() {
      super.redo();
      if (!skipModifying)
        setEntities(afterDeepCopies);
    }
  }

  protected class Remove extends UndoAction {
    protected List<Data> removeBackup;

    public Remove(List<E> l) {
      super(l);
    }

    public List<E> getRemoved() {
      List<E> l = new ArrayList<E>();
      for (Data data : removeBackup)
        l.add(data.entity);
      return l;
    }

    @Override
    public void undo() {
      super.undo();
      addEntities(removeBackup);
    }

    @Override
    public void firstRun() {
      super.firstRun();
      removeBackup = getCopyEntities();
    }

    @Override
    public void redo() {
      super.redo();
      removeEntities(removeBackup);
    }
  }
  • 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-06-18T01:14:41+00:00Added an answer on June 18, 2026 at 1:14 am

    I think you need an extra layer of indirection. Rather than having the other code reference an “A”, you probably want it to reference a “Stack<A>”, and use “peek()” to reference the current value (when you change the value in the array, you will probably want to “push()” the new value).

    For the sake of code cleanliness, however, you can make this a little neater by making “A” into an interface and creating an implementation of “A” (let’s call it “DelegatingStackAImpl”) that will provide the same interface as “A” except with support for “push()” and “pop()” and forwarding invocations to whichever instance is currently on the top of the stack. Thus you won’t break existing uses of “A” while still supporting this extra layer of indirection.

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

Sidebar

Related Questions

I have an ArrayList as an Object(from reflection) and made my own class that
I have an ArrayList containing Objects that have a date value. Now I want
I have an arraylist which contain objects of my own class. I want to
I have a problem with sorting an ArrayList of my own objects. My class
I have an ArrayList of objects in one activity and I need this arrayList
At the moment inside the class I have ArrayList which the objects are stored
I have an ArrayList that I want to use to hold RaceCar objects that
I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have declared two ArrayLists in my main class. One is an ArrayList of
i have an issue with the understanding the arraylist which contain the objects from

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.