Arrays

What is an Array? An array is a container object that holds a fixed number of values of the same type. The length or number of elements of an array is established when the array is initially created. Once the array is created, the length is fixed.

You have actually already seen an example of an array when we created the HelloWorld application back in Lesson 2 that dealt with setting both CLASSPATH and PATH.

Take a look at the diagram below that shows you what happens when you create an array with a length of 10 elements.

Java Array Diagram

Each item contained within the array is called an element. Each element is referenced by its numerical index. As you can see in the above illustration, indexes always start at 0 in Java. Understanding the indexing, the 9th element in the array would be accessed at index position 8.

Creating an array can be done a couple of different ways. We can define an an empty array and then set the size. Or we can define an array and set the size all at one time. Both options are shown below.

//Define an array with no size
int[] jcdArray1;
		
//Allocate space for 10 elements
jcdArray1 = new int[10];
		
//Define and allocate space for 10 all at once
int[] jcdArray2 = new int[10];


Once our array is created, assigning values to each element of the array can be done as follows.

//Assign values to each index or element of the array
jcdArray1[0] = 10;
jcdArray1[1] = 20;
jcdArray1[2] = 30;
jcdArray1[3] = 40;
jcdArray1[4] = 50;
jcdArray1[5] = 60;
jcdArray1[6] = 70;
jcdArray1[7] = 80;
jcdArray1[8] = 90;
jcdArray1[9] = 100;


Using a standard for loop, you can print out each element of the array and confirm that the indexes do indeed start at position 0.

for(int ctr=0; ctr<10; ctr++) {
	System.out.println("Element at position "+ctr+" = "+jcdArray1[ctr]);
}

Element at position 0 = 10
Element at position 1 = 20
Element at position 2 = 30
Element at position 3 = 40
Element at position 4 = 50
Element at position 5 = 60
Element at position 6 = 70
Element at position 7 = 80
Element at position 8 = 90
Element at position 9 = 100


Even though our example array was of type integer, you can declare arrays of different data types:

byte[] jcdByteArray;
short[] jcdShortArray;
long[] jcdLongArray;
float[] jcdFloatArray;
double[] jcdDoubleArray;
boolean[] jcdBooleanArray;
char[] jcdCharArray;
String[] jcdStringArray;


As a cool shortcut, you define an array, set the size, and initialize it with values all at the same time with a nice one liner. Our jcdCoolArray has 10 elements just like the previous array we defined.

int[] jcdCoolArray = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };


The System class in Java has a method called arraycopy() that will allow you to copy one array to another of the same data type. The jcdCoolArray is the source, define an empty array called jcdCoolArrayCopy as the destination, and call arraycopy() to copy the elements from source to destination array.

int[] jcdCoolArray = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
		
int[] jcdCoolArrayCopy = new int[10];
	
System.arraycopy(jcdCoolArray, 0, jcdCoolArrayCopy, 0, jcdCoolArray.length);
		
for(int ctr=0; ctr<10; ctr++) {
	System.out.println("(copy) Element at position "+ctr+" = "+jcdCoolArrayCopy[ctr]);
}

(copy) Element at position 0 = 10
(copy) Element at position 1 = 20
(copy) Element at position 2 = 30
(copy) Element at position 3 = 40
(copy) Element at position 4 = 50
(copy) Element at position 5 = 60
(copy) Element at position 6 = 70
(copy) Element at position 7 = 80
(copy) Element at position 8 = 90
(copy) Element at position 9 = 100


Now that your done with lesson 5, let’s see what you learned with the knowledge check on Java variables.



Add a Comment

Your email address will not be published. Required fields are marked *