Why an ArrayList clear automatically?

Discussion in 'Java' started by guess85, Dec 20, 2007.

  1. guess85

    guess85 Guest

    Hi everybody. I have two ArrayList "corso_list" and "corsoitems_list"
    that I use like temporary array. When I filled up them, I save them in
    two ArrayList that is a property of the object "a" and, at the
    beginning of the while I clear the temp array. When I do this
    operation, even the array in the object a were cleared. Why? Below
    there is the code used. Thanks to all.

    ArrayList corso_list = new ArrayList();
    ArrayList corsoitems_list = new ArrayList();
    while(iter.hasNext())
    {
    corso_list.clear();
    corsoitems_list.clear();
    a=new Assioma();
    i=(Individual)iter.next();
    l=(Literal) (i.getPropertyValue(model.getProperty(modello_conoscenza
    +"nome_elemento_conoscenza")));
    a.setNome(l.getString());
    l=(Literal) (i.getPropertyValue(model.getProperty(modello_conoscenza
    +"testo_assioma")));
    a.setTesto(l.getString());
    a.setTipo(((String)(lista_sottoclassi.get(j))));
    iter1 = i.listPropertyValues(model.getProperty(modello_conoscenza
    +"sono_riferiti"));
    while (iter1.hasNext())
    {
    nome_corso = (String)(((Resource)(iter1.next())).getLocalName());
    corso_temp=model.getIndividual(modello_conoscenza+nome_corso);

    l=(Literal)corso_temp.getPropertyValue(model.getProperty(modello_conoscenza
    +"nome_corso"));
    corso_list.add(l.getString());
    corsoitems_list.add(new SelectItem(nome_corso,l.getString()));
    }
    if ( !corsoitems_list.isEmpty() )
    Collections.sort(corsoitems_list, new
    ObjectComparator("getLabel",true));
    a.setSono_riferiti(corso_list);
    a.setSono_riferitiitems(corsoitems_list);
    temp_list.add(a);
    }
     
    guess85, Dec 20, 2007
    #1
    1. Advertising

  2. "guess85" <> wrote in message
    news:...
    > Hi everybody. I have two ArrayList "corso_list" and "corsoitems_list"
    > that I use like temporary array. When I filled up them, I save them in
    > two ArrayList that is a property of the object "a" and, at the
    > beginning of the while I clear the temp array. When I do this
    > operation, even the array in the object a were cleared. Why? Below
    > there is the code used. Thanks to all.


    Most likely because you're not really saving them. This piece of code

    > a.setSono_riferiti(corso_list);
    > a.setSono_riferitiitems(corsoitems_list);


    probably only saves the references to the lists--it does not create new
    copies of the lists. Every "a" shares the same references to the lists so
    when you clear them you lose the contents. If corso_list, corsoitems_list
    are temporary, put their creation into the loop and skip the clearing
    operations.

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Dec 20, 2007
    #2
    1. Advertising

  3. guess85

    guess85 Guest

    On 20 Dic, 16:39, "Matt Humphrey" <> wrote:
    > "guess85" <> wrote in message
    >
    > news:...
    >
    > > Hi everybody. I have two ArrayList "corso_list" and "corsoitems_list"
    > > that I use like temporary array. When I filled up them, I save them in
    > > two ArrayList that is a property of the object "a" and, at the
    > > beginning of the while I clear the temp array. When I do this
    > > operation, even the array in the object a were cleared. Why? Below
    > > there is the code used. Thanks to all.

    >
    > Most likely because you're not really saving them. This piece of code
    >
    > > a.setSono_riferiti(corso_list);
    > > a.setSono_riferitiitems(corsoitems_list);

    >
    > probably only saves the references to the lists--it does not create new
    > copies of the lists. Every "a" shares the same references to the lists so
    > when you clear them you lose the contents. If corso_list, corsoitems_list
    > are temporary, put their creation into the loop and skip the clearing
    > operations.
    >
    > Matt Humphreyhttp://www.iviz.com/


    Now it runs correctly, thanks.
     
    guess85, Dec 20, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,771
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,428
    Kaidi
    Jan 3, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,071
    Smokey Grindel
    Dec 2, 2006
  4. xz
    Replies:
    16
    Views:
    2,405
  5. Philipp
    Replies:
    6
    Views:
    940
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page