Reputation: 3
In my project, I was given the class given below. It works like, whenever a Flat object is created the array flats
is incremented by one flat object. My question is how can I increment this array every time a flat object is created.
Im really stuck on it. dont know where to start!
thx!
public class Block {
public Block() {
super();
flats = new Flat[0];
}
Flat[] flats;
class Flat {
Room[] rooms;
class Room {
private int width;
private int height;
private int numWindow;
public Room(int w, int h, int n) {
super();
this.width = w;
this.height = h;
this.numWindow = n;
}
@Override
public String toString() {
return width + "/" + height + "/" + numWindow;
}
}
public Flat() {
super();
rooms = new Room[0];
}
}
}
Upvotes: 0
Views: 261
Reputation: 133
Can you sent this array to Flat's Constructor?
Code (based on your code):
public class Block {
private Flat[] flats;
public Block(Flat[] flats) {
this.flats = flats;
}
public Flat[] getFlats() {
return flats;
}
public void setFlats(Flat[] flats) {
this.flats = flats;
}
@Override
public String toString() {
return Arrays.toString(flats);
}
public class Flat {
private String value;
public Flat() {
//init your Flat
this.value = UUID.randomUUID().toString();
flats = Arrays.copyOf(flats, flats.length + 1);
flats[flats.length - 1] = this;
}
@Override
public String toString() {
return value;
}
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Test");
Block block = new Block(new Block.Flat[0]);
block.new Flat();
block.new Flat();
System.out.println(block.toString());
}
}
Output: [b30c54dd-7e5d-46bc-81cf-18172e68337e, c22c4589-7563-4ada-a4eb-16226bd81f45]
Upvotes: 0
Reputation: 42899
Flat
is Block
's inner class, therefore, the flats
member variable is directly accessible from the Flat
class.
As per the documentation:
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.
You can modify the Flat
constructor like this:
public Flat() {
super();
rooms = new Room[0];
// append the newly created flat into the flats array
flats = Arrays.copyOf(flats, flats.length + 1);
flats[flats.length - 1] = this;
}
This means the flats array will be copied for each new flat, which is arguably much less efficient than using an ArrayList.
While this works, I strongly recommend against having deeply nested class structures, and use a better dependency and ownership model. This is outside the scope of this question though.
Upvotes: 1
Reputation: 99
If you want to only count the objects, you don't need an array, you can use a static field called let's say NumOfObjects and increment it every time the constructor is created. However, if you wish to save the objects in an array I recommend you to use ArrayList in this case (as static field in Flat class) (documentation) instead of an array because of the fixed size of arrays in java, you'll have to create an array with fixed initial size and in the Flat constructor you should check if the array is full, you should create bigger array, copy the old one, and add the new object. ArrayList takes care of all that instead of you.
Upvotes: 0
Reputation: 27
Define flats array as static member of the class Block, and in the constructor of Flat push the newly created object.
Upvotes: 0