Chapter 7 – Arrays, Creating and Using Arrays | Introduction to Programming Using Java

Chapter 7 – Arrays, Creating and Using Arrays | Introduction to Programming Using Java

 

Chapter 7 – Arrays

 

Computers 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.

 

Chapter 7 - Arrays, Creating and Using Arrays | Introduction to Programming Using Java

 

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

When 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.

 

Chapter 7 - Arrays, Creating and Using Arrays | Introduction to Programming Using Java

 

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:

 

i 7 Chapter 7 - Arrays, Creating and Using Arrays | Introduction to Programming Using Java

 

then an object of class Person could be considered to be a record with four fields. The field names are name, idnumber, birthday, and age. Note that the fields are of various types:

String, int, and Date.

Because records are just a special type of object, I will not discuss them further.

Chapter 7 - Arrays, Creating and Using Arrays | Introduction to Programming Using Java

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 6 – Introduction to GUI Programming | Introduction to Programming Using Java

 

 

1 thought on “Chapter 7 – Arrays, Creating and Using Arrays | Introduction to Programming Using Java”

Leave a Comment