Free Programming Books
Free download ebooks on computer and programming

Free Java Ebook "Java Collections" Sample Chapter

Java Collections
Free Part One The Historical Collection Classes, Chapter 2: Arrays
Download chapter

The Collections Framework is supplied with all versions of the Java 2 platform and provides programmers with incredibly efficient ways to manipulate data. However, given the large number of methods and classes in this library, using them correctly is hardly a cakewalk. Well-known columnist and bestselling author John Zukowski gives the Java professional exactly what he or she needs to know about this vital library in order to maximize productivity. This practical book contains comprehensive coverage of the important Collections Framework from the working programmer's point of view, while staying away from academic abstractions.

Java Collections leads you through the standard Java support for maintaining abstract groups of data, from the historical collection classes available since the inception of Java time, through the Collections Framework introduced with the Java 2 platform, and on to third-party alternative libraries for times when the standard support isn't enough. If you're working with data in Java programs, you need to understand the Collections Framework. Let Zukowski's Java Collections be your guide!

< < prev 

Arrays

Arrays are the only collection support defined within the Java programming language. They are objects that store a set of elements in an order accessible by index, or position. They are a subclass of Object and implement both the Serializable and Cloneable interfaces. However, there is no .java source file for you to see how the internals work. Basically, you create an array with a specific size and type of element, then fill it up.

NOTE Since arrays subclass Object, you can synchronize on an array variable and call its wait() and notify() methods.

Let's take a look at what we can do with array objects-beginning with basic usage and declaration and moving through to copying and cloning. We'll also look at array assignment, equality checking, and reflection.

Array Basics

Before going into the details of declaring, creating, initializing, and copying arrays, let's go over a simple array example. When creating a Java application, the main() method has a single argument that is a String array: public static void main(String args[]). The compiler doesn't care what argument name you use, only that it is an array of String objects.

Given that we now have the command-line arguments to our application as an array of String objects, we can look at each element and print it. Within Java, arrays know their size, and they are always indexed from position zero. Therefore, we can ask the array how big it is by looking at the sole instance variable for an array: length. The following code shows how to do this:


public class ArrayArgs {

	public static void main (String args[]) {

		for (int i=0, n=args.length; i<n; i++) {

			System.out.println("Arg " + i + ": " + args[i]);

		}

	}

}

NOTE Array indices cannot be of type long. Because only non-negative integers can be used as indices, this effectively limits the number of elements in an array to 2,147,483,648, or 231, with a range of indices from 0 to 231-1.

Because an array's size doesn't change as we walk through the loop, there is no need to look up the length for each test case, as in: for (int i=0; i<args.length; i++). In fact, to go through the loop counting down instead of up as a check for zero test case is nominally faster in most instances: for (int i=args.length-1; i>=0; i-). While the JDK 1.1 and 1.2 releases have relatively minor performance differences when counting down versus counting up, these timing differences are more significant with the 1.3 release.

To demonstrate the speed difference on your platform, try out the program in Listing 2-1 to time how long it takes to loop "max int" times:

Listing 2-1. Timing loop performance.


public class TimeArray {

	public static void main (String args[]) {

		int something = 2;

		long startTime = System.currentTimeMillis();

		for (int i=0, n=Integer.MAX_VALUE; i<n; i++) {

		something = -something;

	}

	long midTime = System.currentTimeMillis();

		for (int i=Integer.MAX_VALUE-1; i>=0; i-) {

			something = -something;

	}

	long endTime = System.currentTimeMillis();

	System.out.println("Increasing Delta: " + (midTime - startTime));

	System.out.println("Decreasing Delta: " + (endTime - midTime));

	}

}

This test program is really timing the for-loop and not the array access because there is no array access.