Re: Java generics

Posted 19 February 2009 - 09:13 PM

The only reason i was insisting on overridding the add method was because i need to put limitations on when items can and cannot be added (dependant on passenger number and weight). Although terrible and it promotes bad java coding, this was the specification of my Aircraft class

Quote

The Aircraft
This must be an object which contains the passengers. You must use a Java Collection object (not an array) to hold the Passengers and this must limit the number of passengers either when the maximum seat number is reached (250 for this demo) or if the maximum weight is reached (21300). An elegant way to do this would be to subclass the Collection class you choose and add extra functionality to it (this will also get you better marks) but you could also just build in the functionality to your Aircraft class. The aircraft should calculate the fare for each passenger as they are added and keep track of the revenue being generated (total fares of all the passengers).

Why on earth you would want to extend a collection is beyond me when there are so many better choices e.g. composition. But for now i suppose i just have to suck it up and do it until my complaints are heard, lol.

BTW, where did you get the source code for the ArrayList class? I was looking everywhere for the JFrame source so i could make a triangle gui, without having to use to horrible looking new transparancy class, but i couldnt find the source anywhere.

So thats the code behind the trimToSize method in ArrayList. I went to the root of Nick's link, and you can get them all from here. I am going through the JFrame code now. This is going to provide too much fun now!! Time too see how java truely works!

Re: Java generics

Posted 19 February 2009 - 10:39 PM

You can download the source for the Sun JDK from the Java.Sun.com You can also find most of it in the OpenJDK website (see the links for source)

You are right that you will have to set the hard limit yourself. The array list will grow to fit the data. I was not really thinking clearly -- Airplanes don't magically grow new seats (despite what JetBlue would like to think). Where as the ArrayList will create new space. So you have to set a limit. You should probably make that a property though rather than a hard-coded value. -- Generally I try to stay away from hard coded values.