{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


Introduction+to+Programming+Using+Java+07 - Chapter 10...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10 Generic Programming and Collection Classes H ow to avoid reinventing the wheel? Many data structures and algorithms, such as those from Chapter 9, have been studied, programmed, and re-programmed by generations of computer science students. This is a valuable learning experience. Unfortunately, they have also been programmed and re-programmed by generations of working computer professionals, taking up time that could be devoted to new, more creative work. A programmer who needs a list or a binary tree shouldn’t have to re-code these data structures from scratch. They are well-understood and have been programmed thousands of times before. The problem is how to make pre-written, robust data structures available to programmers. In this chapter, we’ll look at Java’s attempt to address this problem. 10.1 Generic Programming G eneric programming refers to writing code that will work for many types of data. We encountered the term in Section 7.3, where we looked at dynamic arrays of integers. The source code presented there for working with dynamic arrays of integers works only for data of type int . But the source code for dynamic arrays of double , String , JButton , or any other type would be almost identical, except for the substitution of one type name for another. It seems silly to write essentially the same code over and over. As we saw in Subsection 7.3.3, Java goes some distance towards solving this problem by providing the ArrayList class. An ArrayList is essentially a dynamic array of values of type Object . Since every class is a subclass of Object , objects of any type can be stored in an ArrayList . Java goes even further by providing “parameterized types,” which were introduced in Subsection 7.3.4. There we saw that the ArrayList type can be parameterized, as in “ ArrayList < String > ”, to limit the values that can be stored in the list to objects of a specified type. Parameterized types extend Java’s basic philosophy of type-safe programming to generic programming. The ArrayList class is just one of several standard classes that are used for generic pro- gramming in Java. We will spend the next few sections looking at these classes and how they are used, and we’ll see that there are also generic methods and generic interfaces (see Subsec- tion 5.7.1). All the classes and interfaces discussed in these sections are defined in the package java.util , and you will need an import statement at the beginning of your program to get access to them. (Before you start putting “ import java.util.* ” at the beginning of every program, you should know that some things in java.util have names that are the same as 485
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
486 CHAPTER 10. GENERIC PROGRAMMING AND COLLECTION CLASSES things in other packages. For example, both java.util.List and java.awt.List exist, so it is often better to import the individual classes that you need.) In the final section of this chapter, we will see that it is possible to define new generic classes, interfaces, and methods. Until then, we will stick to using the generics that are predefined in
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 51

Introduction+to+Programming+Using+Java+07 - Chapter 10...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online