Thanks, I guess I was hoping there would be some sort of
trick to get an array of struct like in 'c'
Phil...
"Neomorph" <ne******@nospam.demon.co.uk> wrote in message
news:hc********************************@4ax.com...
On Sat, 30 Aug 2003 09:32:29 GMT, "Phil..." <ry***@ieee.org> two-finger
typed:
I am trying to figure out how to create an array that contains objects
and not references to objects. I know how to do the latter, but not
the former. Any help is appreciated.
Well, what you are doing below, is the way to do it.
If you want the content as primitives (because that's the only way to have
a non-reference array), you would have to split it in advance...
I have the following:
class ArrayTest extends Frame {
public static void main(String args[]) {
<snip>
Jones[] arr; // arr is an array of class Jones objects
That line would split into:
String [] Jones_args ; // this is still a reference, by the way!
char [] Jones_group ;
char [] Jones_type ;
arr = new Jones[5]; // allocate the array
would become:
Jones_args = new String [5] ; // allocate the arrays
Jones_group = new char [5] ;
Jones_type = new char [5] ;
// allocate the objects and fill the array with references to them
for (int i = 0; i < arr.length; i++) {
arr[i] = new Jones();
}
this is superfluous.
arr[0].type = 'c'; // for example
arr[0].group= 'g';
arr[0].args = "1, 4";
would become:
// fill the array with the values
Jones_type[0] = 'c'; // for example
Jones_group[0] = 'g';
Jones_args[0].args = "1, 4";
}
<more snipage>
}
This can then be forgotten:
public class Jones {
public String args;
public char group;
public char type;
}
There is no way like in C, where the structure is repeated in memory within
the array. Classes are always used as instance objects by way of reference.
You could declare the Jones class package and put the source of it in the
same java file as the public main class, or even put it inside the class
definition of the main class (ArrayTest) as an inner class, but then you
have to create an instance of ArrayTest first...
Cheers.