Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Developer
rentepatrick
Posts: 16
Registered: 02-03-2010
My Carrier: Developer

Vector reference

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.titlefr);
                        struct[i].titleen=info.getElement(StoreInfo.titleen);
                        struct[i].titlesp=info.getElement(StoreInfo.titlesp);
                        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.refcatfr);
                        struct[i].refcaten=info.getElement(StoreInfo.refcaten);
                        //System.out.println("Product: "+struct[i].titlefr);
                    }
                }
            }
        PersistentStore.destroyPersistentObject(0x238c02af1ba912f6L);
        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?

Please use plain text.
Developer
simon_hain
Posts: 10,780
Registered: 07-29-2008
My Carrier: O2 Germany

Re: Vector reference

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

peter_strange wrote:
"This process should happen traumatically for you in both JDE and Eclipse."
Please use plain text.
New Developer
rentepatrick
Posts: 16
Registered: 02-03-2010
My Carrier: Developer

Re: Vector reference

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?

Please use plain text.
Developer
JCarty
Posts: 1,028
Registered: 01-25-2009

Re: Vector reference

[ Edited ]

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() );
}

 

 

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Please use plain text.