   [java] Dynamic arrays and clasess
  Greetings m8ts.I have an array of class abc with null/zero/unknown space ([]), which i want to allocate on the fly, any ideas  how it can be done?I have this codeQuote:class abc{String text;}public class Program{public static void main(String[] args){//Works, but is not what i need.abc test = new abc();test.text = "Sample text";//Here is what i need, (TestArray)abc[] ta;ta = new abc[2];ta[0].text = "Hello!";ta[1].text = "World!";}}This just does not work for me, my program is always crashing.   
  Try this instead: Allocate the objects before writing to them:class abc {  String text; }  public class Program{  public static void main(String[] args){    //Works, but is not what i need.   abc test = new abc();   test.text = "Sample text";    //Here is what i need, (TestArray)   abc[] ta;   ta = new abc[2];   ta[0] = new abc();   ta[0].text = "Hello!";   ta[1] = new abc();   ta[1].text = "World!";  } }The array by itself holds only references to objects, not the objects itself. So you need to allocate the objects and then setting the references to the object. Just then you could access the objects by using the references from the array.   ;  Thank you, it worked!p.s. Wow, that was a fast answer :D   ;  one more question, what should i do if i put it in a loop:abc[] ta;for (int i = 0; i < 10; i++){//This line is destroying all current datata = new abc[i+1];ta = new abc();ta.text = "No"+i;}Expected output is:No1No2No3..current output:nullnull...No9Is there any good way to just append new data?   ;  like you said "This line is destroying all current data"you should use a temp array of length +1 fill that array an then put the temp array in ta//but since you already know that after the for loop you'll have an array of 10abc[] ta = new abc[10];for (int i = 0; i < 10; i++){ta = new abc();ta.text = "No"+i;}//You could also create a constructor for your classclass abc{String text;//this is your constructorabc(String s){text = s;}}//and use...ta = new abc("No"+i);...[Edited by - Iarus on December 1, 2005 2:14:18 PM];  What if i hava a random/dynamic value - not 10 every time?Should i just create a copy of array, modify custom array, and finaly create Full array with current custom array and copied array?   ;Quote:Original post by LuckyOneWhat if i hava a random/dynamic value - not 10 every time?Are you not allowed to use anything out of the Collections framework ?  Something like an ArrayList would be far superior to arrays. However, if you must use arrays, the simplest way is to create the new array, copy the old data into it, and assign it to the old array.abc[] taNew = new abc[ ta.length + 1 ];// copy the dataSystem.arraycopy( ta, 0, taNew, 0, ta.length );// append the datataNew[ ta.length ] = new abc( "No" + Integer.toString( someNumericValue ) );// reassign the old arrayta = taNew;;  If you need frequently changing the size of the array you should take into account to use java.util.ArrayList (stormrunner mentioned it already). It grants an indexed access, appending, insertion, and also trimming.Your example would becomeimport java.util.ArrayList;ArrayList ta = new ArrayList();for (int i = 0; i < 10; i++)  {ta.add("No"+i);  }simply adding the new element always at the end.If you're using Java 1.5 (compiler compliance level 5.0) you should use generics like here:import java.util.ArrayList;ArrayList<String> ta = new ArrayList<String>();for (int i = 0; i < 10; i++)  {ta.add("No"+i);  }This declares ta as an ArrayList holding Strings (in the pre 1.5 version one could declare it only to hold Objects). Of course you could then store only Strings into it; everything else would be denied at compile time. That gives another level of security that one does not store unwanted object types. It also has some coding advantages when iterating using java.util.Iterator.   ;  what would be the diference beteewn ArrayList and Vector? both are dynamic arrays. Is an ArrayList a Vector + a LinkList??Why/When would I need to use one instead of the other?   ;  Vector was around first, and is thread safe, whereas ArrayList was introduced later and isn't thread safe.You almost always want to use an ArrayList, as the thread safety costs you a little speed. If you really do need a thread safe ArrayList then use List list = Collections.synchronizedList(new ArrayList(...));More detail in the javadocs, obviously.   