i have an Array of objects and want to add an ArrayList of a different ob

could someone please help me out with this one,

when the table is created i need create one new pile to each element of this array

create one new block for each pile

add a block to the pile

the block is an arraylist

the block and pile classes have already been set up

public class Table

{

private int NPILES;// the number of piles

private Pile[] piles; // the array of piles

/** Create a table and initialise the array piles.

* For every pile created create a block

*/

public Table(int npiles)

{

// Initialise NPILES

NPILES = npiles;

// Create the array of size NPILES

piles = new Pile[NPILES];

for (int i = 0; i < NPILES; i++) {

}

}

/** Print the table. */

public void print()

{

System.out.println("");

System.out.println("P = Piles : B = Blocks");

System.out.println("");

System.out.println("P : B");

// Print the sequence of piles

for (int i = 0; i < NPILES; i++) {

System.out.println(i +":");

// Print pile i (as shown in the project specification

// ... to be completed

}

}

}

so it prints

Piles : Blocks

0 : 0

1 : 1

2 : 2

3 : 3

4 : 4

any help would be great sorry about being sooo plain with words im new to java

Message was edited by:

JohnnyMac

[1445 byte] By [JohnnyMaca] at [2007-10-3 3:32:37]
# 1

where are the blocks to be added?

> for (int i = 0; i < NPILES; i++) {

Add the blocks here

>}

>

> for (int i = 0; i < NPILES; i++) {

> System.out.println(i +":");

> // Print pile i (as shown in the project

> specification

> // ... to be completed

>}

> }

should have another loop inside the for-loop to print all values in the block at position i in the pile

r035198xa at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 2
Yeah i realise that the blocks need to be added in the constructor but i dont know how to do it!!
JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 3
NPILES = new Pile();
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 4
thats how far ive come for (int i = 0; i < NPILES; i++) {Pile pile = new Pile();Block block = new Block(i);}}
JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 5
> piles[i] = new Pile();Sorry. Forgot the code tag and messed up your variable names.
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 6
ok but now i need to get it to print both the blocks and piles
JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 7

>thats how far ive come

>for (int i = 0; i < NPILES; i++) {

>Pile pile = new Pile();

>Block block = new Block(i);

>}

>}

what is supposed to be in the blocks since they are supposed to be arraylists?

>Block block = new Block(i);

do you really want to pass i in the constructor for Block?

do you know how many blocks you are supposed to create or are getting the blocks from somewhere?

r035198xa at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 8
you have the same amount of blocks as u do piles
JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 9
> ok but now i need to get it to print both the blocks> and pilesHow many piles?I thought you are supposed to have only one pile and many blocks
r035198xa at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 10
this is the first stage of the programinitially its set up like0: 01: 12: 23: 34: 4left being the piles right side being the blocksthen u will invoke a meathod to pile block a over block b and soo on
JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 11
Piles are swet by the user as NPILES
JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 12

> you have the same amount of blocks as u do piles

I see.

(for int i = 0;i < piles.length;i++) {

System.out.println("Pile"+i);

for(int j = 0;j < pile[i].length;j++) {

System.out.println(""+block.get(j));

}

}

r035198xa at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 13

for(int j = 0; j < pile.length;j++) {

error returns cannot find symbol variable pile

for (int i = 0; i < piles.length;i++) {

System.out.println("Pile"+i);

for(int j = 0; j < pile.length;j++) {

System.out.println(""+block.get(j));

}

}

JohnnyMaca at 2007-7-14 21:26:53 > top of Java-index,Java Essentials,Java Programming...
# 14
Congratulations. You'll make a great programmer.And use code tags, please.
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:54 > top of Java-index,Java Essentials,Java Programming...
# 15
fixed that now its same line but error is variable length
JohnnyMaca at 2007-7-21 10:12:39 > top of Java-index,Java Essentials,Java Programming...
# 16

for (int i = 0; i < piles.length;i++) {

System.out.println("Pile"+i);

for(int j = 0; j < pile[i].length;j++) {

System.out.println(""+block.get(j));

}

}

[code]

JohnnyMaca at 2007-7-21 10:12:39 > top of Java-index,Java Essentials,Java Programming...
# 17

Hey JonnyMac, we are only trying to help you do the programming

sorry about the variable name error i was only trying to show you how to print values from an array of arraylists. (i am not justifying the error however).

change everywhere else in the code to the proper spelling

what does the error say now

Message was edited by:

r035198x

r035198xa at 2007-7-21 10:12:39 > top of Java-index,Java Essentials,Java Programming...
# 18
{for(int j = 0; j < piles[i].length;j++) {for that code it has a error cannot find symbol variable length
JohnnyMaca at 2007-7-21 10:12:39 > top of Java-index,Java Essentials,Java Programming...