Add multiple objects to a list

Asked

Viewed 1,183 times

9

I’m developing an application web, where I have a list of an object called frames, well, every time I click on a button I add a new object to that list, only the problem is this: When I try to add a new object for the second time to that list, the first object that was inserted previously disappears, as if nothing was inserted, leaving only the 2nd inserted object and not the 1st and 2nd, ie, is always guarding the last inserted object. How do I save as many objects as needed to add to this list?

Well I have it so far, when the user click add, it will open a screen to fill in some information about which object I mentioned: This is the button:

  <p:commandLink id="btn_close_users_modal3" actionListener="#{messageBean.insertFrame()}" 
                                               styleClass="btn btn-default" >
                                    <i class="fa fa-plus fa-fw" /> #{bundle['system.ui.label.add.frameAdd']}
                                </p:commandLink>

And that’s my method, where what you have to do and then add to a list.

public void insertFrame() {
    try {
        // caso nao visualize a mensagem e salve direto, chama de qualquer forma esse metodo
        teste();

        frame.setContent(svg);
        frame.setWriteContent(objSvg.getValueText1() + " / " + objSvg.getValueText2());
        frame.setRemoved("f");

        frame.setOrder(1);
        frame.setLogo('f');

        listAllFrames.add(frame);

        MessageGrowl.info(MessageProperties.getString("message.sucesso"));
        RequestContext.getCurrentInstance().execute("PF('framesModal').hide();");

        //     frameFacade.save(frame);
    } catch (Exception e) {

        e.printStackTrace();
    }

}
  • Ivan, try to make available what you have already done so far, this ensures greater attention to your question, if possible edit your question with a minimum example to reproduce the question.

  • "List" is an ambiguous term in a Java context. Is that a List, or an Arraylist, or something else? Post code and highlight or simulate the specific problem, otherwise it is difficult to give a concrete answer.

  • I edited the question, I think it was clearer.

  • Your listAllFrames is instantiated as?

  • listAllFrames = new Arraylist<>(); first I was instantiating, then I withdrew this method there

  • Ivan, if you were instantiating within the method every call of the method is a new Arraylist created, and dispensed with when the method returns. The correct thing is that you install it as class property. You better post all Class code, or at least more details relevant to this process.

Show 2 more comments

2 answers

1

I believe that what happens is the reuse of the object called frame.

In your code, at no time do you create a new object to call frame; you simply keep using the same object for everything. Java works at the object reference level, therefore in the list listAllFrames the various references to these objects are stored. So, if you added the object to the list and then edited that object, when you take the list, you’ll see that the object in the list has also undergone the same change.

Try this code down here for better understanding:

class SeguraNumero {
    public int numero;
}

void reaproveitandoObjeto() {
    ArrayList<SeguraNumero> l = new ArrayList<>();
    SeguraNumero segura = new SeguraNumero();

    for (int i = 0; i < 5; i++) {
        segura.numero = i;
        l.add(segura);
    }
    for (SeguraNumero segura: l) {
        System.out.println("segurou o numero " + l.numero);
    }
}


void novosObjetos() {
    ArrayList<SeguraNumero> l = new ArrayList<>();

    for (int i = 0; i < 5; i++) {
        SeguraNumero segura = new SeguraNumero();
        segura.numero = i;
        l.add(segura);
    }
    for (SeguraNumero segura: l) {
        System.out.println("segurou o numero " + l.numero);
    }
}

The function reaproveitandoObjeto is analogous to what you wrote, already the novosObjetos does not reuse the previous objects; I believe that you should have done something more analogous to novosObjetos

0

This may be happening because the program is interpreting the object that is in the list and the object that you are adding, as the same object! Thus, it just changes the memory reference of the object, removing it from the previous position and placing it in the current position you entered.

Try to make a copy of the object before adding it to the list, so you will have 2 objects and not 1 only!

Example:

Frame f = new Frame();
f = frame;
listAllFrames.add(f);
  • On the second line, you destroyed the Frame created in the first line...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.