Jump to content
  • Advertisement
Sign in to follow this  
LuckyOne

[java] Dynamic arrays and clasess

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 code
Quote:
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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 data
ta = new abc[i+1];
ta = new abc();
ta.text = "No"+i;
}



Expected output is:
No1
No2
No3
..
current output:
null
null
...
No9

Is there any good way to just append new data?

Share this post


Link to post
Share on other sites
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 10
abc[] 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 class
class abc
{
String text;

//this is your constructor
abc(String s)
{
text = s;
}
}

//and use
...
ta = new abc("No"+i);
...


[Edited by - Iarus on December 1, 2005 2:14:18 PM]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by LuckyOne
What 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 data
System.arraycopy( ta, 0, taNew, 0, ta.length );
// append the data
taNew[ ta.length ] = new abc( "No" + Integer.toString( someNumericValue ) );
// reassign the old array
ta = taNew;

Share this post


Link to post
Share on other sites
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 become

import 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!