October 15, 2009

Arrays

Arrays
Arrays are to data as loops are to control flow.
Overview

The basic solution. Arrays are the basic way to store large numbers of values. Every programming language you're likely to use has arrays as part of the fundamental language, altho with many little variations on how they work.

Strings are like arrays of chars and you should be familiar with the idea of storing multiple values which are accessed by a numeric index.

Other possibilities. Arrays in Java are great for working with a fixed numbers of elements, but Java also has the Collections library of classes that are a better choice when working with a variable number of objects, eg, ArrayList, but it's essential to learn about arrays, so we'll start with them.
Arrays store many values using one name and an index

An array can store many similar values in memory. Each value is accessed by specifying an integer subscript or index in brackets following the array name. "Array" in Java means approximately the same thing as array, matrix, or vector does in math. Unlike math and some other programming languages, in Java you must both declare an array and allocate a fixed amount of memory for it.
Declaring an array

An array variable is like other variables -- you must declare it, which means you must declare the type of elements that are in an array. All elements must be the same type. Write the element type name, then "[]", then the name of the array variable. The declaration allocates only enough space for a reference to an array (typically 4 bytes), but doesn't create the actual array object.
String[] args; // args is an array of Strings
int[] scores; // scores is an array of ints
JButton[] controlButtons; // controlButtons is an array of JButtons

No size in declaration. Unlike some languages, never put the size of the array in the declaration because an array declaration specifies only the element type and the variable name. The size is specified when you allocate space for the array.
Names - Plurals or collective nouns are most common for array names

Most programming guidelines suggest using plural names, or nouns denoting a collection of things, for arrays and other collections of multiple values. This is not a rigid rule, and your choice will often be based on linguistic sensitives. For example, you might have an array of words and their frequencies. It would be entirely appropriate to name this wordFrequencyTable because the word "table" suggests many entries. If you had an array of single words, you might call it words or wordList. Naming the array using the singular word would probably be very confusing to most readers.

Examples in this text often follow the common convention of using the array variable name "a". This seriously violates the rule of having meaningful names, so please don't adopt this textbook-example style in your code!
Allocate an array object with new

Create an array using new. This example creates an array of 100 int elements, from a[0] to a[99].
int[] a; // Declare a to be an array of ints
a = new int[100]; // Allocate an array of 100 ints

These are often combined in one line.
int[] a = new int[100]; // Declare and allocate.
Subscripts (indexes/indices)

Subscripts are enclosed in square brackets []. xi in mathematics is x[i] in Java, and is pronounced "x-sub-i".

Subscript ranges always start at zero because Java came largely from C++, which had a good reason for using zero (pointer arithmetic on arrays). It isn't the way that humans normally count; you'll just have to live with it.

Java always checks subscript legality to be sure the subscript is >= 0, and less than the number of elements in the array. If the subscript is outside this range, Java throws ArrayIndexOutOfBoundsException. This is far superior to the behaver of C and C++, which allow out of range references. Consequently, Java programs are far less susceptible to bugs and security flaws than C/C++ programs.

Zero-based indexing is a constant annoyance of Java's zero-based indexing. The natural human value for hours or days should be used as an index to make the program most readable, but this would mean starting with 1, not zero.

Translate or ignore 0 element? If the data you are working with naturally starts at one, not zero, either the data must be modified before using it as an index, or the size of the array could be increased by one row so that the natural values could be used, and row 0 would never be referenced. Ignoring the zeroth array element instead of translating data to be zero based is a style decision, and you'll find various positions on this matter. See the AutoSales example below.
Length of an array

Each array has a constant (final) instance variable that has its length. You can find out how many elements an array can hold by writing the array name followed by .length. In the previous example, a.length would be 100. Remember that this is the number of elements in the array, one more than the maximum subscript.

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts