I have a simple question ?
String[] names = null ;
names[0] = "Hello"
I'm getting an error ..
How could I instantiate array as I don't know the the size limit... help me
|
How about this?
Or you might also use EDIT: For J2ME: There was a trick posted here for dynamic array of Ints. I suppose it should possible to convert it for Strings. I have converted that example, however I didn't have J2ME emulator to test it:
|
|||||
|
Use Edit: as you can't use Vector and ArrayList, you'll have to roll you own implementation of dynamic array. You'll find one almost ready with some explanations on algolist.net. Simply replace the int storage by a String storage.
|
|||||||||||||
|
try
Use an
looks like j2me has non-generic ArrayList that you could use like this.
|
|||||||||
|
Sorry, that cannot be done. Arrays are of fixed size in Java, and you have to give the size when you create the array. If you need a flexible buffer, consider using an ArrayList instead. That will grow as needed. |
|||
|
If you don't know the size limit (or more generally: almost always) you'll want to use a
The reason you're getting an exception (a You'd have to initialize it like this:
|
|||
|
As you explained yo want to use it at J2ME there is no ArrayList provided for J2ME however there is an implementation here: http://kickjava.com/src/j2me/util/ArrayList.java.htm You can try it. Also you should consider here too: http://www1.j2mepolish.org/javadoc/j2me/de/enough/polish/util/ArrayList.html |
|||||||
|
As you are on J2ME and say you can't use arraylist I don't see you having any choice. You need to choose a reasonable starting size for your array, watch the size, and if you need to add more objects than the size, copy it to a larger array. With our constraints I cannot think of another way. |
||||
|
you can use with vector
like this.. Hope this helpfull |
|||
|
|||
|