Introduction+to+Programming+Using+Java+04

Introduction+to+Programming+Using+Java+04 - Chapter 7...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 7 Arrays C omputers get a lot of their power from working with data structures . A data structure is an organized collection of related data. An object is a data structure, but this type of data structure—consisting of a fairly small number of named instance variables—is just the beginning. In many cases, programmers build complicated data structures by hand, by linking objects together. We’ll look at these custom-built data structures in Chapter 9. But there is one type of data structure that is so important and so basic that it is built into every programming language: the array. An array is a data structure consisting of a numbered list of items, where all the items are of the same type. In Java, the items in an array are always numbered from zero up to some maximum value, which is set when the array is created. For example, an array might contain 100 integers, numbered from zero to 99. The items in an array can belong to one of Java’s primitive types. They can also be references to objects, so that you could, for example, make an array containing all the buttons in a GUI program. This chapter discusses how arrays are created and used in Java. It also covers the standard class java.util.ArrayList . An object of type ArrayList is very similar to an array of Objects , but it can grow to hold any number of items. 7.1 Creating and Using Arrays W hen a number of data items are chunked together into a unit, the result is a data structure . Data structures can be very complex, but in many applications, the appropriate data structure consists simply of a sequence of data items. Data structures of this simple variety can be either arrays or records . The term “record” is not used in Java. A record is essentially the same as a Java object that has instance variables only, but no instance methods. Some other languages, which do not support objects in general, nevertheless do support records. The C programming language, for example, is not object-oriented, but it has records, which in C go by the name “struct.” The data items in a record—in Java, an object’s instance variables—are called the fields of the record. Each item is referred to using a field name . In Java, field names are just the names of the instance variables. The distinguishing characteristics of a record are that the data items in the record are referred to by name and that different fields in a record are allowed to be of different types. For example, if the class Person is defined as: class Person { String name; 313 314 CHAPTER 7. ARRAYS int id number; Date birthday; int age; } then an object of class Person could be considered to be a record with four fields. The field names are name , id number , birthday , and age . Note that the fields are of various types: String , int , and Date ....
View Full Document

This document was uploaded on 08/10/2011.

Page1 / 60

Introduction+to+Programming+Using+Java+04 - Chapter 7...

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

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