Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 48
Registered: ‎12-11-2008
My Device: Not Specified

"method for Array resizing"

HI guys,

 

I have one string array. the length  that array is 10 "myarr(10)"... but it have values at 0 to 7 index only.... last 3 array index are null. Just i need to resize the array .from array(10) to myarray(7)..............

In .net i am used "Redim preserve()"..... let me know if u know systax for resize string() ............

 


Expect .......u
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: "method for Array resizing"

AFAIK there is no resize array method but I'm not sure if that matters too much. You could either use a vector

or just leave the extra elements as null.If you have a lot of these things but have a decent

upper bound on the size ( not a lot of wasted space), it may be easier to just iterate until you

hit a null or "resize" in the process of doing something else.

 

New Developer
Posts: 48
Registered: ‎12-11-2008
My Device: Not Specified

Re: "method for Array resizing"

hi ,

 

Thanks for ur reply......... But still am not clear...........

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: "method for Array resizing"

Java arrays cannot be resized.

 

Use java.util.Vector class instead of arrays. Vector is resizable.

 

If you insist on arrays - use  java.util.Vector to collect values and after that pass them to the array.

 

Like that:

 

 

Vector myVector = new Vector(); myVector.addElement("Amelie"); myVector.addElement("Ann"); myVector.addElement("Eva"); myVector.addElement("Mary"); myVector.addElement("Theresa"); // ...... some processing here // passing values to array final int arraySize = myVector.size(); String[] myArray = new String[arraySize]; for (int i=0; i<arraySize; i++) { myArray[i] = (String)myVector.elementAt(i); } // freeing vector memory myVector.removeAllElements(); //removing unecessary references myVector = null; //recycling myVector memory

 


 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: "method for Array resizing"

I wouldn't give up just like that, while I'm sure much of Vector is implemented in native code,

arrays may have reduced extra stuff and seem to work better with some other things

like the sorting classes ( yes, I've seen the sorted vector thing). It may or may not be a big

deal, I just decided that in most cases I could convert things to arrays if you have any idea

a priori how big they will be and don't mind a null which can be checked about as easily as a vector

size.

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: "method for Array resizing"

The way that you can "resize" an array in Java is to create a new array of the correct size and use System.arrayCopy() to copy the contents from one to the other.