Tutorials
Arrays
Introduction
- An array is a collection of similar objects. Actually, one of two things:
- A collection of primitives, all of the same type
int[] integers; integers = new int[20];
- The first line declares a reference to an array of ints, called integers.
- The second line allocates space for an array of twenty ints and makes integers point to this newly allocated array.
- A collection of references, all of the same type
Integer[] integers; integers = new Integer[20];
- The first line declares a reference to an array of Integer references, called integers.
- The second line allocates space for an array of twenty references to Integer objects and makes integers point to this newly allocated array.
- Each element in the array is currently referencing
null
since we haven't created anyInteger
objects. - The following loop will create twenty
Integer
objects and assign the references in the array to point to the newly created Integer objects:for(int i=0; i<integers.length; ++i) { integers[i] = new Integer(i); }
- A collection of primitives, all of the same type
- Arrays are nice because they allow you to store a collection of items in a group and act on all of the items in the group in the same way. For example, consider the following method:
public static void display(String[] names, PrintStream out) { for(int i=0; i<names.length; ++i) { out.println(names[i]); } }
- We can call
display
as follows:String[] names = {"Peter", "Paul", "Mary-Anne"}; display(names, System.out); // Note: System.out is a PrintStream object
- The method will display all three names. If
names
had twenty names in it, all twenty would be displayed. - If we wanted to do the same thing without an array, we'd need to hard-code the number of names, like this...
public static void display(String name1, String name2, String name3, PrintStream out) { out.println(names1); out.println(names2); out.println(names3); }
- I'm not going to show you what it would look like if we had twenty names because I don't want you to lose your lunch, but just thinking about it is probably enough to make you nauseous.
- We can call
- Arrays can be cumbersome because they cannot be resized easily. If we need to add an element to an array that is already full, we would need to create a larger array like this:
- Create a new, larger, array
- Copy over all the primitive values/references in the old array to the new array
- Reassign the reference to the original array so that it now points to the new array
/** * Resizes an array of Strings * @param array Array to be resized * @param size Desired size for the array * @return A reference to the newly sized array */ public static String[] resize(String[] array, int size) { String[] newArray = new String[size]; // Since the size could be less than array.length, we need to make sure // we only go up to the smaller of the two values. for(int i=0; i<array.length && i<size; ++i) { newArray[i] = array[i]; } return newArray; }
- Wait, wouldn't it be nice if we could just start out with an empty array and then just add elements to it as needed? Check this out because the girls and boys at Sun Microsystems have already fulfilled your wildest dreams!1)
Examples
Consider the following two methods. Both of these methods reverse the order of the elements of an array, and yet they are quite different.
public static double[] reverse1(double[] data) { double[] temp = new double[data.length]; for(int i=0; i<data.length; i++){ temp[i] = data[data.length-1-i]; } return temp; } public static double[] reverse2(double[] data) { for(int i=0; i<data.length/2; i++){ int j = data.length-1-i; double temp = data[i]; data[i] = data[j]; data[j] = temp; } return data; }
How do reverse1()
and reverse2()
differ?
1)assuming your wildest dreams are about easily adding elements to a collection of items.
Last modified: Monday, 29-Jul-2024 06:55:10 EDT