How can I store all objects created in a class in a single array returned by that class in Java? -
i using java create object called track, contains bunch of different numerical objects created methods in class (floats, doubles, ints, etc. etc). class, track, instantiated multiple times class (multiple instances of track inside single instance of class). want track become array of number objects parent class contain multiple track arrays.
the track class structured , nested inside class have loop instantiate track many times needed.
class track { public int trackid() { } public short trackvalidity() { } ....//other objects }
like declare arrays, example:
track[] track = new track[42]; track[0] = new track();
or full working example:
public class objectarray { track[] track; public static void main(string[] args){ objectarray tester = new objectarray(); tester.setit(); tester.getit(); } public void setit(){ track = new track[42]; for(int i=0; i<42; i++){ track[i]= new track(i); track[i].calcproduct(i, i+1); } } public void getit(){ for(int i=0; i<track.length; i++){ system.out.println("track "+track[i].getserial()+" has product: "+track[i].getproduct()); } } class track{ private int serial; private int product; track(int i){ serial = i; } public void calcproduct(int a, int b){ product = a*b; } public int getserial() { return serial; } public int getproduct() { return product; } } }
Comments
Post a Comment