Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why an ArrayList clear automatically?

Reply
Thread Tools

Why an ArrayList clear automatically?

 
 
guess85
Guest
Posts: n/a
 
      12-20-2007
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_cono scenza
+"nome_elemento_conoscenza")));
a.setNome(l.getString());
l=(Literal) (i.getPropertyValue(model.getProperty(modello_cono scenza
+"testo_assioma")));
a.setTesto(l.getString());
a.setTipo(((String)(lista_sottoclassi.get(j))));
iter1 = i.listPropertyValues(model.getProperty(modello_con oscenza
+"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.getPr operty(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);
}
 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      12-20-2007

"guess85" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/


 
Reply With Quote
 
 
 
 
guess85
Guest
Posts: n/a
 
      12-20-2007
On 20 Dic, 16:39, "Matt Humphrey" <(E-Mail Removed)> wrote:
> "guess85" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear Arraylist vs new ArrayList Philipp Java 6 05-28-2008 09:07 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments