Learn Java
Arrays
Properties of Arrays
- Fixed size
- Can store references to objects or primitive values
- Are homogeneous - all elements in an array have to be of the same type.
- Are indexable - every element in an array is accessed by an integer index
Declaring an array variable - example
String[] weekdays;
The variable "weekdays" is declared to reference an array holding references to String objects
boolean[] answer;
The variable "answer" is declared to reference an array holding references to boolean primitive values;
Creating an array
weekdays = new String[7];
A new array with seven components holding references to String objects is created and assigned to the previously declared variable "weekdays"
Declaration and creation combined
String[] weekdays = new String[7];
Creating and initialising arrays
String[] weekdays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
The variable "weekdays" is declared and assigned to a new array of length 7, populated with references to string literals.
Car[] bigCar = {new Car(), new Car(), newCar()};
The variable "bigCar" is declared to hold references to Car objects and assinged to a newly created array where each component is initialised with a reference to a newly initialised Car object.
Putting elements into an array
Create an array as follows
double[] numbers = new double[5];
All numbers of type double are initialised to 0.0
To assign a value to component 2 do the following
numbers[2] = 20.4;
Storing non-literal objects in an array
Create an array as follows
Car[] bigCar = new Car[4];
Here is a way to put a reference to a newly initialised Car object into the array at index 2
Car[2] = new Car();
It is legal to assign an object to a variable declared to be of the type of one of its superclasses, this is called substitutability So if FordCar is a subclass of Car then the follow is a legitimate assignment
Car bigCar = new FordCar();
The same applies to array components as follows
Car[] bigCar = new Car[2];
bigCar[0] = new FordCar();
To find the length of an array execute bigCar.length;
Iterating through an array
To increment each element of an array of integers
for (int i = 0; i < anArray.length; i++) { anArray[i] = anArray[i] + 1; }
The foreach statement
The foreach statement is used to access a copy of each element in an array in turn. For example.
String[] carMakes = {"ford", "bmw", "audi"}; for (String eachCarMake : carMakes) { System.out.println(eachCarMake); }
Making an exact copy of an array
int[] myArrayCopy = new int[myArray.length]; for (int i=0; i < myArray.length; i++) { myArrayCopy[i] = myArray[i]; }
Two dimensional arrays
To declare a variable to reference a two-dimensional array to hold int elements and assign a newly created two dimensional array to that variable, do something like the following
int[][] twoDim = new int[4][6];
This creates an array with 4 components, each component holds an array of length 6
twoDim[1][3] = 5; puts a 5 in component 3 of array component 1
Declaring a literal two dimensional array
int[][] table = {{2, 1, 7,}, {4, 13, 9}};
java.util.Arrays
In Java there is a library called java.util.Arrays
Examples of static methods within this utility class
Arrays.toString(intArray); Returns a string representation of the argument array
Arrays.toString(objectArray); Returns a string representation of the argument array
Arrays.deepToString(intArray); Returns a string representation of a multi-dimensional array
Arrays.equals(firstIntArray, secondIntArray); Returns true if the two array arguments are equal, false otherwise
Arrays.deepEquals(firstMultiDimArray, secondMultiDimArray); Returns true if both multi-dimensional arrays are equal, false otherwise
Arrays.sort(intArray); Sorts the argument, an array of integers into ascending mumerical order
Arrays.sort(stringArray); Sorts the argument, an array of String objects into alphabetical order
Arrays.fill(intArray, 2); Assigns the integer value 2 to each component of the array
Arrays.fill(intArray, 2, 5, 22); Assigns the integer value 22 to the sub-array of intArray, starting from index 2 (inclusive), ending at the index 5 (exclusive)
Previous Page | Page 6 | Next page |
Page 1 2 3 4 5 6 7 |