Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2446
| Newsgroups | comp.lang.java.help |
|---|---|
| Date | 2013-01-13 21:39 -0800 |
| References | <f2b43fa0-1da2-4dc4-981b-bb756186050b@googlegroups.com> <kcvsdk$eua$1@dont-email.me> |
| Message-ID | <fdc77c55-c090-4156-91e3-ba16da5ca976@googlegroups.com> (permalink) |
| Subject | Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. |
| From | Lew <lewbloch@gmail.com> |
markspace wrote:
> GCRhoads wrote:
class Piece
{
public byte type;
public byte loc;
}
class Config
{
public Config( int n ) { piece = new Piece[ n ]; }
private Piece[] piece; // array whose size is not known at compile time
private int next;
private int parent;
}
class QHT
{
private Config[] conf;
public QHT (int m, int n );
}
You should actually copy and paste your code from working code.
Which this isn't. Now if you have issues, then "working" instead
means "produces the error of interest".
>> In the constructor QHT, I want to allocate to "conf" an array of m Configs
conf = new Config[m];
>
>> where each Config contains an array of n "Piece"s. The values of m and n
For each array entry assign it a
Config config = new Config(n);
>> are not known at compile time only at run time.
That is intrinsic to constructor and method arguments.
> Two dimensional array?
To GCRhoads: Bear in mind that "two-dimensional array" in Java is actually
slang.
In truth there is no "two-dimensional array" exactly. What there is is a
one-dimensional array of arrays. Each array element of the "outer" array
points to a separate "inner" array, or to 'null'. Iterating through the
outer array represents moving to the next row. Iterating through each
inner array represents moving to the next column. Each inner array might
be of a different length, or 'null'.
> public class Qht {
> private Piece[][] conf;
The first dimension, or pair of square brackets, is the outer dimension,
the row dimension. The second dimension is the inner, or column dimension.
> public Qht( int m, int n ) {
> conf = new Piece[m][];
'm' rows, each of unknown number of columns or 'null'. (Initially 'null'.)
When you initialize an array using dimensions this way, each array element
is initialized to the appropriate zero-like value: zero, 'false' or 'null'
depending on the array type. So markspace's example shows how you create
an array of 'm' elements, each of type 'Piece []', or "array of 'Piece'",
initialized to 'null'.
You have to loop through the outer array to give each element its value.
> for( int i = 0; i < m; i++ ) // just wrote "that loop" again
> {
> conf[i] = new Piece[n];
Once again, he shows creation of an array, this time of length 'n', each
element of which is initialized to 'null'.
> }
> }
>
> // rest of class here...
> }
Unfortunately this doesn't quite fit the original question.
You wanted an array of 'Config' objects, yes?
And each 'Config' itself contains an array as a member.
This holds the same knowledge as markspace's data structure. However the use of types is arguably easier to follow is more expressive.
public void doSomething()
{
Config [] configs = new Config [m];
etc.
--
Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Find similar | Unroll thread
Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-13 17:38 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-13 18:56 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-14 13:36 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-14 16:59 -0500
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-14 20:00 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-14 20:04 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Patricia Shanahan <pats@acm.org> - 2013-01-14 23:44 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-15 09:07 -0500
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-15 09:06 +0200
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-15 08:32 -0500
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Lew <lewbloch@gmail.com> - 2013-01-15 11:19 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Joshua Cranmer <Pidgeot18@verizon.invalid> - 2013-01-15 13:52 -0600
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Roedy Green <see_website@mindprod.com.invalid> - 2013-01-15 20:56 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. markspace <markspace@nospam.nospam> - 2013-01-13 19:01 -0800
Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Lew <lewbloch@gmail.com> - 2013-01-13 21:39 -0800
csiph-web