Programming References and tutorials for Developers.

Learning java array | Java Array Tutorial

By The Saint on Sunday, January 11, 2009

Filed Under:

Learining Java Array

What is an array? An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

It means that, if I have to store multiple integer numbers I can simply declare a single integer array and simply specify its dimension, To make more clear I will how it works:

First I will discuss how arrays are declared in the program

Array Source code:

Syntax:
       <data type> <array-name>[] = new <data type>[<size n>];
-Or-
int projectuno[] = new int[5];


public class IntNumbers
{

public static void main(String args[])
{
int num[] = new int[7];
}

}

That’s quite simple.

Array containing 5 integer values of type int called projectuno could be represented like this:




Inserting a value in an Array

Inserting a value in an array is quite simple you simply invoke its array name plus the index.

Ex.:
projectuno[0] = 1;
projectuno[1] = 10;
projectuno[2] = 100;
projectuno[3] = 1000;

projectuno[4] = 200;
projectuno[5] = 20;
projectuno[6] = 2;

By doing this you simply filled up all the index of the array projectuno.




Note:
All values added in the array must be on the same data type.
Error: projectuno[0] = “Hello”;

Specifying an index higher than the declared size can create errors in the program.
Error: projectuno[10] = 300;

Index of an Array don’t accept negative value.
Error: projectuno[-1] = 400;

Accessing a value in an Array
We can access a value in array by accessing its index the same way we did as we add a value in an array. Like an ordinary variable we can access the value individually.

Example:
System.out.println(projectuno[0]);
System.out.println(projectuno[1]);
System.out.println(projectuno[2]);
System.out.println(projectuno[3]);
System.out.println(projectuno[4]);
System.out.println(projectuno[5]);
System.out.println(projectuno[6]);



Sample Problem:
Create a program that will display 7 integer numbers using array.
Ouput:
1
10
100
1000
200
20
2

Solution:
public class IntegerNum
{
public static void main(String args[])
{
int projectuno[] = new int[7];
projectuno[0] = 1;
projectuno[1] = 10;
projectuno[2] = 100;
projectuno[3] = 1000;
projectuno[4] = 200;
projectuno[5] = 20;
projectuno[6] = 2;

System.out.println(projectuno[0]);
System.out.println(projectuno[1]);
System.out.println(projectuno[2]);
System.out.println(projectuno[3]);
System.out.println(projectuno[4]);
System.out.println(projectuno[5]);
System.out.println(projectuno[6]);
}
}



Multidimensional Array
A multidimensional array consists of two or more arrays defined by sets of array elements, Each set of array elements is an array. The first set of array elements is considered the primary array, and the second and subsequent sets of array elements are considered subarrays.

Two dimensional Array
Two dimensional Array can be viewed as matrix or table.

Syntax in declaring a Two dimensional Array

data type array_name = new type[row size][col size];

Example:
int projectuno[][] = new int[2][4];









Inserting a value in Two dimensional Array

Inserting a value in an array is quite simple you simply invoke its array name plus the index for row and column.

Example:
projectuno[0][0] = 2;
projectuno[0][1] = 4;
projectuno[0][2] = 6;
projectuno[0][3] = 8;
projectuno[1][0] = 3;
projectuno[1][1] = 6;
projectuno[1][2] = 12;
projectuno[1][3] = 15;


Accessing a value in an Array
We can access a value in multidimensional array by accessing its indexes the same way we did as we add a value in an array.

Example:
System.out.println(projectuno[0][0]);
System.out.println(projectuno[0][1]);
System.out.println(projectuno[0][2]);
System.out.println(projectuno[0][3]);
System.out.println(projectuno[1][0]);
System.out.println(projectuno[1][1]);
System.out.println(projectuno[1][2]);
System.out.println(projectuno[1][3]);

1 comments for this post

Anonymous

how to make a simple array.. into a a java.?? can you teach me???

Posted on October 4, 2010 at 1:34 PM