Page 457 - Introduction to Programming with Java: A Problem Solving Approach
P. 457
Since ArrayLists and standard arrays are both inefficient when it comes to inserting or removing an element to or from the interior of a list, if you’re doing a lot of inserting and removing, you should consider a different structure—a linked list. A linked list is a sequence of elements, where each element contains a data item plus a reference (a “link”) that points to the next element. You can create a linked list using ex- actly the same procedures you used to create an ArrayList. Just replace the ArrayList class with the LinkedList class. If you’re interested in LinkedList details, look up the LinkedList collection class on Sun’s Java API Web site.
The above table says that a standard array is more efficient than an ArrayList when it comes to storing primitive values. Why is that? Remember that before an ArrayList stores a primitive value, it must wrap the primitive in a wrapper object. That wrapping process takes time, and that’s the cause of the inefficiency.
Summary
• Arrays facilitate the representation and manipulation of collections of similar data. You access array elements with <array-name>[index], where index is a nonnegative integer, starting at zero.
• You can create and completely initialize an array in one statement, like this: <element-type>[] <array-name> = {element0, element1, ...};
• Usually, however, it’s more useful to defer element initialization and use new to create an array of un- initialized elements, like this:
<element-type>[] <array-name> = new <element-type>[array-size];
• You can read or write directly to an array element by inserting an appropriate index value in square
brackets after the arraAy npamaegatoany tiPmeDafFter thEe anrrahyahasnbecenecreated.
• Every array automatically includes a public property called length, which you can access directly
withthearrayname.Thehighestindexvalueis<array-name>.length - 1.
• To copy an array, you copy each of its elements individually, or you can use the System.arraycopy
method to copy any subset of elements in one array to any location in another array.
• A histogram is an array of elements in which each element’s value is the number of occurrences of
some event.
• A sequential search is a good way to search for a match in an array whose length is less than about 20,
but for long arrays, you should first sort the array with the Arrays.sort method and then use a bi-
nary search.
• A two-dimensional array is an array of arrays, declared with two sets of square brackets after the
element-type identification. You can instantiate it with an initializer or with new followed by element
type and two array-size specifications in square brackets.
• In creating an array of objects, multiple instantiations are required. After instantiating the array, you
also need to instantiate the individual element objects within the array.
• If you need to repeatedly insert or delete elements within an array, you should consider using an
ArrayList rather than a standard array. When you declare or instantiate an ArrayList for a group of Car elements, you should include the type of elements it will contain in angled brackets, like this:
ArrayList<Car> car = new ArrayList<Car>();
• An ArrayList stores objects only. Java automatically makes necessary conversions between primi- tives and wrapped primitives, so you don’t have to worry about that, but if you want an ArrayList of primitives like int, you must declare it with the wrapped type, like this:
ArrayList<Integer> num = new ArrayList<Integer>();
• You can pass objects to and from methods anonymously.
• Use a for-each loop to iterate through a collection of data items.
Summary 423