Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java arrays

Arrays are a very important concept in Java as well as many other programming languages. An array is a special type of variable which can store a list of values.

This tutorial focuses on:

The necessity of arrays

Imagine you are writing a program that has three variables which store vowels. Your code for these three variables would probably look like this:

char vowel1 = 'A'; char vowel2 = 'B'; char vowel1 = 'C';

With Arrays, it is much simpler. An array gives you the ability to group together related variables into one set. Arrays are special variables which can store a list of values. Therefore, instead of the code above you can declare a 'vowels' array:

char[] vowels = new char[3]; vowels[0] = 'A'; vowels[1] = 'B'; vowels[2] = 'C';

Declaring arrays

Arrays are declared with a data type, an array name, and square brackets [] specifying that you are declaring an array.

Syntax:
datatype arrayName[]; OR datatype[] arrayName;
Example:
int evenNumbers[]; can also be declared as int[] evenNumbers;

After an array is declared, an array object has to be assigned to it using the new keyword as well as the length of the array - specified in the square brackets. The length of the array denotes how many elements an array can hold.

Syntax:
arrayName = new datatype[numElementsInArray];
Example:
evenNumbers = new int[10];

The above example declares an array named evenNumbers of data type int which has a length of 10, therefore it can store 10 elements.

Adding values to an array

Add values to an array by referring to the appropriate index of the array and assigning it a value.

Syntax:
arrayName[index] = value;
Example:
evenNumbers[3] = 20;

The above example will assign the value 20 to the 4th element in the evenNumbers array.

NOTE: Array indexes begin at 0, so the first element of an array is at index 0, the second element of an array is at index 1, and so on.

As an alternative to declaring an array and then assigning values to its elements, you can do both tasks together.

Syntax:
dataType[] arrayName = {value, value, value, value, etc.};
Example:
char[] vowels = {'A', 'E', 'I', 'O', 'U', 'Y'};

The above example declares an array named vowels of data type char which stores six elements.

Accessing an arrays elements

You can access an arrays elements by referring to the array by its name and the appropriate index number of the element you wish to access.

Example:
char[] vowels = {'A', 'E', 'I', 'O', 'U', 'Y'}; System.out.println("Here is a vowel: " + vowels[3]); System.out.print("Here is another vowel: " + vowels[5]);
Output:
Here is a vowel: O Here is another vowel: Y

Modifying an arrays elements

You can modify an arrays elements by referring to the array by its name and the appropriate index number of the element you wish to modify.

Syntax:
arrayName[index] = newValue;
Example:
int oddNumbers[] = new int[4]; oddNumbers[0] = 1; oddNumbers[1] = 3; oddNumbers[2] = 5; oddNumbers[3] = 7; System.out.println("oddNumbers[0]: " + oddNumbers[0]); System.out.println("oddNumbers[2]: " + oddNumbers[2]); //change some array values oddNumbers[0] = 15; oddNumbers[2] = 19; System.out.println("new value of oddNumbers[0]: " + oddNumbers[0]); System.out.println("new value of oddNumbers[2]: " + oddNumbers[2]);
Output:
oddNumbers[0]: 1 oddNumbers[2]: 5 new value of oddNumbers[0]: 15 new value of oddNumbers[2]: 19

Getting the length of an array

The length of an array is the number of elements in it. To get the length of an array, use the length property of the Array object with the array whose length you want to find out.

Example:
int oddNumbers[] = new int[4]; oddNumbers[0] = 1; oddNumbers[1] = 3; oddNumbers[2] = 5; oddNumbers[3] = 7; System.out.print("The length of the oddNumbers array is " + oddNumbers.length);
Output:
The length of the oddNumbers array is 4
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information