SIGN UP MEMBER LOGIN:    
ARTICLE

Arrays in Java

Posted by Marcus Articles | Java July 14, 2011
An array is a fundamental concept in core Java and is widely used in Java development. So, lets start an interesting topic.
Reader Level:

An Array is a collection of homogeneously-typed variables (same-typed variables) that share a name. Two types of arrays are:
1. One Dimensional Arrays.
2. Multi Dimensional Arrays.

ONE DIMENSIONAL ARRAYS
Declaration Syntax:
Type Variable-Name[];
OR
Type[] Variable-Name;
For e.g.,


Int a[];

Note: The above line of code declares that an array of integer 'a' is declared, however no such array yet exists. Because the array has not been initialized.
An arry must be initialized by using the new keyword, i.e. allocation of memory for the array.
Initialization Syntax:
Variable-Name = new Type[Size];
Now, each element of an array will be initialized to a corresponding value, as in the following table.
Type Value after Initialization
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
char null
boolean false

Example,
a = new Int[5];

In the above example, each element of the array 'a'  is initialized to 0.
A simple program, using Array:
public class JavaArrays {
public static void main(String[] args) {
int DOB[];
DOB = new int[3];
//Above two lines may be written as "int DOB[] = new int[3]".
DOB[0] = 23;
DOB[1] = 6;
DOB[2] = 1990;
System.out.println("My Date of Birth is " + DOB[0] + "-" + DOB[1] + "-" + DOB[2] + ".");
}
}

Output:
My Date of Birth is 23-6-1990.

The same output could be achieved by this easier and simpler version of the above program:
public class JavaArrays {
public static void main(String[] args) {
int DOB[] = {23, 6, 1990};
System.out.println("My Date of Birth is " + DOB[0] + "-" + DOB[1] + "-" + DOB[2] + ".");
}
}

Assigning and Printing values to an array using for loop
public class JavaArrays {
public static void main(String[] args) {
int a[] = new int[4];
for (int i = 0; i < 4; i++) {
// Assigning values to a[]
a[i] = 12 + i;
}
for (int i = 0; i < 4; i++) {
// printing values to a[]
System.out.println(a[i]);
}
}
}

Output:
12
13
14
15

MULTIDIMENSIONAL ARRAYS

Multidimensional arrays are also known as arrays of arrays. Multidimensional arrays have more than one indexes.

Declaration and Initialization Syntax
Type Variable-Name[][]; //Declaration
OR
Type[][] Variable-Name;
Variable-Name = new Type[Size][Size]; //Initialization
Type Variable-Name[][] = new Type[Size][Size]; //Declaration & Initialization
For Example,
public class JavaArrays {
public static void main(String[] args) {

//Allocating a array of 2 by 4 and assigning it to 'a'.

int a[][] = new int[2][4];

int count = 1;

//for loop to fill the array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = ++count;
}
}

//for loop to print the array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

Output:
2 3 4 5
6 7 8 9

Understanding how the preceding 2-Dimensional Array is filled up:
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)

Left Index shows the row and Right Index shows the column.
Inner for loop runs 4-times for each and every time outer for loop runs, i.e. outer loop runs for rows and the inner loop runs for column.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks

Posted by Tom Lewis Jul 17, 2011

Nice Work

Posted by Angelina Erin Jul 15, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor