Summary: Arrays

This lesson summarizes the major points we made in this chapter.

  • An array is a construct in a programming language that enables us to group items having the same or related data types.
  • Declaring an array defines a variable and gives the data type of the array’s entries.
  • Creating an array allocates memory for a fixed number of entries.
  • The number of elements allocated for an array is called the array’s length. The length of the array a is written in Java as a.length.
  • Each element in an array contains at most one value, or entry. Thus, the length of an array is also its capacity. If we place fewer entries into an array than its capacity, the array is said to be partially full.
  • To reference an array element, we write the array’s name followed by an integer expression enclosed in square brackets. The expression is known as an index.
  • Frequently, we will use a loop to process the entries in an array.
  • Assigning one array variable to another does not copy the array. Rather, it results in two names for the same array. The names are said to be aliases.
  • To copy an array, we can call one of the methods System.arraycopy or Arrays.copyOf. Alternatively, we can write a loop that copies each entry in one array to another array.
  • A copy of an array of primitive values contains those values.
  • A copy of an array of objects contains references to those objects. If the original array and the copy reference the same set of objects, the copy is known as a shallow copy. If we duplicate the objects, and each array references its own set of objects, the copy is called a deep copy.
  • Two arrays are equal if they contain the same entries in the same order. Two arrays are identical if they use the same memory locations.
  • We can use the operator == to test whether two arrays are identical but not whether they are equal.
  • To see whether two arrays are equal, we can either call the method Arrays.equals or write our own code that compares the entries in the arrays.
  • We can pass an entire array as an argument to a method. The parameter for such a method has the form data_type[ ] array-name. The corresponding argument is simply the array name.
  • A method can return an entire array. The method’s return type is given in its header as data_type[ ].

Get hands-on with 1200+ tech skills courses.