Java array basics

An array is a data structure that allows you access and manage many variables using a uniform name. Imagine it like a pigeon hole, with rows and columns, you can identify a specific location in a pigeon by addressing it using row and column coordinates; Now imagine how that can be a useful thing if you have many pigeon holes, a ware house full of pigeon holes, you have the ability to address a single pigeon hole, by referencing the name of the pigeon hole, then the row and column coordinates. Think of the Java array this way, then the rest is just mechanics.

Declararing Java arrays is just like declaring any other variable, except it has an additional notation, it’s got a pair square braces either prefixed or suffixed to the variable name, like this.

String args[]

Remember that from the Hello World sample, the main() function’s signature has to accept an array of String objects (from the command line). Some ground rules first on the how to declare an array:

The pair of braces can be located either to right of the variable name or to the left of the variable name
The white spaces before, in between or after the square braces are irrelevant, it’s ignored by the Java compiler. Hence, these declarations are valid


String args[] //right of the variable name
String []args //left of the variable name
String[] args //still left of the variable name, white spaces are not important
                 //remember?

Basic usage of arrays

Declare any type of variable as an array by affixing square braces to the variable name. When you initialize the array, you have to define it’s size, it’s the number of elements that the array can contain (how many holes in the pigeon hole), the size is defined putting an integer value in between the braces of the type (right hand side of the equal sign).

String i[] = new String[4];

This array can carry 5 string objects, but the numbering will start from zero (0), not 1, so the elements are numbered 0,1,2,3,4. The last index will be 4, not 5. The last index is size of the array (5) less 1.

This array is empty right now, it doesn’t contain anything, because we haven’t put anything yet, this is the case when you declare arrays of reference types, it’s a different thing when you declare arrays of primitive types — the array is automatically filled using default values of primitive types. For example;

int i[] = new int[5];

array i is automatically filled with zeroes from element 0 all the way to the last element. If you declared an array of long, short or byte, it will be filled with zeroes also. If you declared an array of double or float, it will be filled with 0.0, all the way. If you declared an array of boolean, it will be filled with false, and for char, it will be filled with ‘ ‘.

In our String array sample, we will have an array of type String, but all 5 elements will contain null. You have to put values inside them, which is easy, like this;


		String i[] =   new String[5];

		i[0] = "The ";
		i[1] = "quick ";
		i[2] = "brown ";
		i[3] = "fox ";
		i[4] = "jumps ";
Array access

You can access the individual elements of the array via the variable, then specify the index (the ordinal position), like so;

String a = i[0];

The code simply declares a variable of String type, named “a”, and were extracting the first element (index 0) of array i, then assigning it to “a”.

Here’s another code.


//
//

class Foo {

	public static void main(String []args) {

		String a = "The quick brown fox jumps over the head of the lazy dog";
		String b[] = a.split(" ");

		for (int i = 0; i < b.length ; i++) {
			System.out.println(b[i]);
		}
	}
}

a.split(” “) – a method of the String class, it basically tokenizes, splits, the string into words using the white space as separator, then each and every word in the tokenized String is stored into an array named “b”.

b.length = Arrays in Java are basically objects, hence they have methods and properties, “length” is one of the properties of an array object (any array), it gives you the size of the array; Don’t confuse this with the length() method of String, in the array, length is a property, in the String class, it’s a method.

If you want to show appreciation for my efforts dear reader, you could buy me a tall hazel nut Americano ($2) via PayPal. Thanks
Post navigation
(previous post)
(next post)
| | | | .

{1 Comment below .. you can add one }


simon 9.30.2009at 02:45

Pretty good tutorials. Very easy to follow. Great job and thanks for sharing

Leave a comment