Java Reference
In-Depth Information
287
Chapter 7 Arrays and Array Lists
C HAPTER G OALS
ȗ To become familiar with using arrays and array lists
ȗ To learn about wrapper classes, auto-boxing, and the enhanced for loop
ȗ To 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 collect values in a data
structure. The most commonly used data structures in Java are arrays and array lists.
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 technical section on copying array values.
287
288
7.1 Arrays
In many programs, you need to manipulate collections of related values. It would be
impractical to use a sequence of variables such as data1 , data2 , data3 , ș, 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. For example, here is how you
construct an array of 10 floating-point numbers:
new double[10]
The number of elements (here, 10) is called the length of the array.
Search WWH ::




Custom Search