Add multiple objects to a list

9

I'm developing a web application 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, but the problem is this: When I try to add a new object for the 2nd time to this list, the first object that was previously inserted disappears, as if nothing was inserted, only the 2nd object being inserted, not the 1st and 2nd objects, ie , you are always saving the last inserted object. How do I save as many objects as I need to add to this list?

Well I have this so far, when the user clicks on add, will open a screen to fill 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 this is my method, in which what you have to do and then adds 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();
    }

}
    
asked by anonymous 23.06.2015 / 03:25

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 continue to use the same object for everything. Java works at the object reference level, so in the listAllFrames list the various references to these objects are saved. So if you added the object to the list and then edited that object, when you get the list, you will see that the list object has also undergone the same change.

Try this code from below for a 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 reaproveitandoObjeto function is analogous to what you wrote, since novosObjetos does not reuse the previous objects; I think you should have done something more like novosObjetos

    
16.05.2017 / 13:04
0

This may be occurring because the program is interpreting the object that is in the list and the object that you are adding, like the same object! Thus, it only changes the object's memory reference, removing it from the previous position and placing it in the current position that you inserted. Home 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);
    
23.06.2015 / 20:48