Chapter 7. Arrays and Array Lists
CHAPTER GOALS
To become familiar with using arrays and array lists
To learn about wrapper classes, auto-boxing, and the enhanced
for
loopTo study common array algorithms
To learn how to use two-dimensional arrays
To understand when to choose array lists and arrays in your programs
To implement partially filled arrays
T To understand the concept of regression testing
In order to process large quantities of data, you need to have a mechanism for collecting values. In Java, arrays and array lists serve this purpose. In this chapter, you will learn how to construct arrays and array lists, fill them with values, and access the stored values. We introduce the enhanced for
loop, a convenient statement for processing all elements of a collection. You will see how to use the enhanced for
loop, as well as ordinary loops, to implement common array algorithms. The chapter concludes with a discussion of two-dimensional arrays, which are useful for handling rows and columns of data.
Arrays
In many programs, you need to manipulate collections of related values. It would be impractical to use a sequence of variables such as value1, value2, value3
, . . ., and so on. The array construct provides a better way of storing a collection of values.
An array is a sequence of values of the same type. The values that are stored in an array are called its "elements". For example, here is how you construct an array of 10 floating-point numbers:
new double[10]
Note
An array is a sequence ...
Get Big Java, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.