So, granted I'm pushing the envelope of my limited Java knowledge. I would like to have an array of objects within a class. I thought this was as subclass but that's not what I want. I don't know what I want. Here is the java docs example.
public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods (getters/setters removed)
// here is my new thought - not sure if its right
public class DriveChain {
public int big-chainring
public int little-chainring
public int chain
// and getters and setters
}
// here i want to create an array of this.
ArrayList<DriveChain> dcArray ;
// here i can add to the array
public void addDriveChain(drivechain dc) {
this.dcArray.add(dc);
}
}
I want to add fields with getters and setters within this class and treat it as an array list. For example as above. Hope I am making sense.
public void addDriveChain(DriveChain dc) { if(dcArray == null) { dcArray = new ArrayList<DriveChain>(); } this.dcArray.add(dc); }dcArrayshould befinaland initialized to an empty list. That avoids the needless complexity of your snippet.