02-11-2010 10:49 AM
Hi everybody!
I have a problem with the attribution of one Vector.
When I do:
Vector plats;
plats = vect;
it seems like I use the two vectors, because in my code I remove all elements of Vector plats and it will automatically remove all elements of my Vector vect. I don't understand why.
Here I post the code:
public void processingInstruction (String target, String data) throws SAXException
{
}
//saving Data into memory device
public StructMeal[] getdata() {
StructMeal[] struct;
struct = new StructMeal[_platsData.size()];//structure
synchronized (store) {
_platsData = (Vector) store.getContents();
if (!_platsData.isEmpty()) {
for(int i=0; i<_platsData.size();i++){
struct[i] = new StructMeal();
StoreInfo info = (StoreInfo)_platsData.elementAt(i);
struct[i].id=info.getElement(StoreInfo.id);
struct[i].titlefr=info.getElement(StoreInfo.title
struct[i].titleen=info.getElement(StoreInfo.title
struct[i].titlesp=info.getElement(StoreInfo.title
struct[i].descfr=info.getElement(StoreInfo.descfr
struct[i].descen=info.getElement(StoreInfo.descen
struct[i].descsp=info.getElement(StoreInfo.descsp
struct[i].ft=info.getElement(StoreInfo.ft);
struct[i].devise=info.getElement(StoreInfo.devise
struct[i].idtva=info.getElement(StoreInfo.idtva);
struct[i].refcatfr=info.getElement(StoreInfo.refc
struct[i].refcaten=info.getElement(StoreInfo.refc
//System.out.println("Product: "+struct[i].titlefr);
}
}
}
PersistentStore.destroyPersistentObject(0x238c02a
getSortedByPricedata(_platsData); // I send the vector _platsData
return struct;
}
public StructMeal[] getSortedByPricedata(Vector vect) {
Vector plats;
plats = vect;
StructMeal[] struct;
struct = new StructMeal[plats.size()];
float bigger=(float) -1.00;
String id = null,titlefr= null,titleen= null,titlesp= null,descfr= null,descen= null,
descsp= null,devise= null,idtva= null,refcatfr= null,refcaten= null;
StoreInfo _info;
int dim = struct.length-1;
int index=0;
for(int i=0;i<plats.size();i++)
{
for(int k=0;k<plats.size();k++)
{
_info = (StoreInfo)plats.elementAt(k);
float num=Float.parseFloat(_info.getElement(StoreInfo.ft
if(num>=bigger)
{
bigger=num;
id = _info.getElement(StoreInfo.id);
titlefr = _info.getElement(StoreInfo.titlefr);
titleen = _info.getElement(StoreInfo.titleen);
titlesp = _info.getElement(StoreInfo.titlesp);
descfr = _info.getElement(StoreInfo.descfr);
descen = _info.getElement(StoreInfo.descen);
descsp = _info.getElement(StoreInfo.descsp);
devise = _info.getElement(StoreInfo.devise);
idtva = _info.getElement(StoreInfo.idtva);
refcatfr = _info.getElement(StoreInfo.refcatfr);
refcaten = _info.getElement(StoreInfo.refcaten);
index=k;
}
}
plats.removeElementAt(index); // here I remove elements of my plats vector
struct[dim] = new StructMeal();
struct[dim].id = id;
struct[dim].titlefr = titlefr;
struct[dim].titleen = titleen;
struct[dim].titlesp = titlesp;
struct[dim].descfr = descfr;
struct[dim].descen = descen;
struct[dim].descsp = descsp;
struct[dim].devise = devise;
struct[dim].idtva = idtva;
struct[dim].refcatfr = refcatfr;
struct[dim].refcaten = refcaten;
struct[dim].ft= Float.toString(bigger);
dim--;
bigger=(float) -1.00;
i=0;
if(plats.size()==1)
i=-1;
}
Can anybody understand why?
02-11-2010 10:53 AM
you only have a reference to an object. if you copy that reference you have two references to the same object.
if you want to clone a vector in j2me/bb you have to copy all elements, one after another.
read about java object references maybe.
02-11-2010 10:59 AM
Sorry, but I don't understand your point, if I have only references, why my vector _platsData change when I change the content of vector plats?
Can you explain me please!
How can i solve this problem?
I want to keep the data into _platsData, but when I delete elements from plats it will delete all elements from _platsData too.
Do you understand me?
02-11-2010 02:08 PM - last edited on 02-11-2010 02:09 PM
Loop through each object in the vector and make a clone of it. As Simon said, you are holding a reference to the "element" in both vectors. You have to make a copy of it so that when you modify one, it doesnt' affect the other.
Vector myFirstData = new Vector();
Vector mySecondData = new Vector();
// Add some elements to myFirstData
for( int i = 0; i<myFirstData.size(); i++ ) {
mySecondData.addElement( myFirstData.elementAt(i).clone() );
}