<< Chapter < Page Chapter >> Page >

Not quite as powerful as an iterator

Although not shown in Listing 3 , the use of an iterator allows you to remove the most recently accessed element from a collection. As near as I cantell, the enhanced for loop does not provide that capability. Therefore, the enhanced for loop is not quite as powerful as an iterator. However, if you don't need to remove elements from the collection, the enhanced for loop appears to be a good and somewhat simpler alternative to an iterator.

The output

The code in Listing 4 produced the last three lines of text in the output shown in Figure 5 . Obviously the last three lines match the first three lines since they simply represent different ways to produce a textrepresentation of the same three Date objects.

Run the programs

I encourage you to copy the code from Listing 5 through Listing 9 Compile the code and execute it. Experiment with the code,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

In this module, I explained:

  • How the Java Collections Framework behaved prior to the release of Java version 1.5.
  • The effect of an incorrect cast in code that doesn't use generics.
  • How to avoid the requirement to cast through the use of generics, including an illustration of some of the required syntax for generics.
  • The compile-time type safety provided by the use of generics.
  • The syntax requirements for the use of iterators with generics.
  • The use of the enhanced for loop with collections.

What's next?

Future modules in this series will be based on the Generics (Updated) section of The Java Tutorials from Oracle. (In the event that these links become broken, you should have no difficulty finding the Oracle material with an online search.)

Those modules will teach you about other aspects of generics including:

  • Generic methods
  • Generic classes
  • Raw types in generics
  • Wildcards and bounded wildcards in generics
  • The impact of inheritance on generics

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Getting Started with Generics
  • File: Java4210.htm
  • Published: 09/01/2013
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Questions & Answers

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
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