473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

array of objects not references to objects

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.
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
arr = new Jones[5]; // allocate the array
// allocate the objects and fill the array with references to them
for (int i = 0; i < arr.length; i++) {
arr[i] = new Jones();
}
arr[0].type = 'c'; // for example
arr[0].group= 'g';
arr[0].args = "1, 4";
}
<more snipage>
}

public class Jones {
public String args;
public char group;
public char type;
}
Jul 17 '05 #1
2 10838
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.
Jul 17 '05 #2
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.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
2
by: Neelesh | last post by:
Hi all, References are (almost always) implemented as constant pointers. We can certainly have an array of constant pointers. But then why we cannot have array of references? I can think of...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
2
by: subramanian100in | last post by:
From http://groups.google.com/group/comp.lang.c++/browse_frm/thread/d5da6e5e37fd194d/6e2e8424a1cfbd2b#6e2e8424a1cfbd2b the following portion is taken. "Mike Wahler"...
31
by: siddhu | last post by:
why can't we have array of references.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.