Tutorialslot.com

youtube facebook twitter gmail pinterest

Arrays In Java

An array is a group of like-typed or similar typed variables that are referred to by a common name. Any type of arrays can be created and it can have one or more dimensions.Array index is used for accessing any specific element.Using array is more convenient as it helps in grouping related information.
One-Dimensional Arrays:
A list of like-typed variables are called as one-dimensional arrays. To create an array, you first need to create an array variable of the desired type. The general form of a one-dimensional array declaration is
data_type variable_name[ ];

For example:
// Declaration of an array
int array[] = new int[4];

// Initializing array elements
array[0] = 1;
array[1] = 4;
array[2] = 3;
array[3] = 2;

//Display array elements
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);

//Using for loop to display elements
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
//println is used to print line in another line
}

Multidimensional Arrays:
In Java, multidimensional arrays are actually arrays of arrays.To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a two dimensional array variable called value.

int val[][] = new int[4][5];

This allocates a 4 by 5 array and assigns it to value. Internally this matrix is implemented as an array of arrays of int.

Example : This example shows how to declare initialize and display multi dimensional array.

// Declaration of multi dimensional array
int multiarray[][] = new int[2][3];

// Initializing elements
multiarray[0][0] = 1;
multiarray[0][1] = 2;
multiarray[0][2] = 3;
multiarray[1][0] = 4;
multiarray[1][1] = 5;
multiarray[1][2] = 6;

//Display array elements
System.out.println(multiarray[0][0]);
System.out.println(multiarray[0][1]);
System.out.println(multiarray[0][2]);
System.out.println(multiarray[1][0]);
System.out.println(multiarray[1][1]);
System.out.println(multiarray[1][2]);

// Or Use for loop to display elements
for (int i = 0; i < multiarray.length; i = i + 1)
{
for(int j=0; j < multiarray[i].length; j = j + 1)
{
System.out.println(multiarray[i][j]);

}
}
Alternative way of Array Declaration:
There is a second form that may be used to declare an array:

data-type[ ] var-name;

Here, the square brackets follow the type specifier, and not the name of the array variable.

For example, the following two declarations are equivalent:
int x[] = new int[3];
int []y = new int[3];

The following declarations are also equivalent:
char wod1[][] = new char[4][4];
char[][] wod2 = new char[4][4];

when declaring several arrays at the same time. For example,
int[] num1, num2, num3; // create three arrays

creates three array variables of type int. It is the same as writing
int num1[], num2[], num3[]; // create three arrays



Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

Java java tutorials learn java java programming tutorial learn java online learning java professionals tutorials Multithreading Generics Object Oriented Language Methods Polymorphism Inheritance