<< Chapter < Page Chapter >> Page >
Figure 4 - A compiler error.
Generics04.java:34: error: no suitable method found for add(String) var1.add("abcd");^ method ArrayList.add(int,Date) is not applicable(actual and formal argument lists differ in length) method ArrayList.add(Date) is not applicable(actual argument String cannot be converted to Date by method invocation conversion)1 error

Without the use of generics, a reference to an object of any type could be added to the collection. This could result in a runtime error laterif the programmer expected an object of type Date when in fact the object is of type String . If you are going to write programs containing errors, compiler errors are almost always preferable to runtimeerrors.

Generic iterator syntax and the enhanced for loop

Iterators

Listing 7 and Listing 8 showed the syntax that you must use to cause a collection object to be treated as a generic collection. You must also usea special syntax when working with generic iterators and the Java Collections Framework.

The enhanced for loop

Another new feature of Java version 1.5, referred to by Oracle as an enhanced for loop, can be used in certain situations to provide most of the benefits of an iterator with a somewhat simpler syntax.

(The enhanced for loop is also sometimes referred to as a for-each loop.)

The program named Generics05 , which begins in Listing 1 , illustrates both of these concepts. A complete listing of the program is provided in Listing 9 .

Listing 1 - Beginning of the program named Generics05.
import java.util.*; public class Generics05{//Create an ArrayList object suitable for // storing references to Date objects.ArrayList<Date>var1 = new ArrayList<Date>();

The main method

As in the previous programs, the main method instantiates an object of the Generics05 class and calls the runIt method on that object. You can view the main method in Listing 9 near the end of the module.

Instantiate a generic ArrayList object

This program is a little longer than the previous programs, so I will break it down and explain it in fragments.

Listing 1 shows the beginning of the Generics05 class. The code in Listing 1 instantiates a new ArrayList object capable of storing references to objects of type Date only. The code in Listing 1 also saves that object's reference in a generic instance variable named var1 . This is the same syntax that you have seen in previous listings.

Populate the collection

Listing 2 shows the beginning of the runIt method. This code populates the ArrayList object with references to three Date objects. The first object encapsulates the current date and time.The second object encapsulates the date and time one day later than the first. The third object encapsulates the date and time two days later than the firstobject.

Listing 2 - Beginning of the runIt method.
void runIt(){ //Get current date and time in milliseconds.long now = new Date().getTime(); //Get length of one day in millisecondslong oneDay = 24 * 60 * 60 * 1000; //Populate the ArrayList objectvar1.add(new Date(now)); var1.add(new Date(now + oneDay));var1.add(new Date(now + 2 * oneDay));

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask