0% found this document useful (0 votes)
87 views

Arrays

Arrays are collections of values of the same type. An array's length cannot be changed after declaration. Arrays can be one-dimensional or multi-dimensional. One-dimensional arrays are arrays of a single type, while multi-dimensional arrays are arrays of arrays. For example, a 2D array is an array whose elements are 1D arrays, and it can represent data in rows and columns. A 3D array contains 2D arrays as its elements.

Uploaded by

wonderkidisgreat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Arrays

Arrays are collections of values of the same type. An array's length cannot be changed after declaration. Arrays can be one-dimensional or multi-dimensional. One-dimensional arrays are arrays of a single type, while multi-dimensional arrays are arrays of arrays. For example, a 2D array is an array whose elements are 1D arrays, and it can represent data in rows and columns. A 3D array contains 2D arrays as its elements.

Uploaded by

wonderkidisgreat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Arrays

An array is a collection of a fixed number of values or objects of a single type. Once declared, the length of an array
cannot be altered. The values or objects contained in an array are called elements and they are accessed by the
common name (i.e. the array name). Arrays can be one dimensional or multi-dimensional. The array elements are
accessed by their numerical indexes. Indexes start from 0, i.e. the index of the first element is 0.

One Dimensional Array: - The general form of the declaration of a one dimensional array is

datatype arrayName[];
or
datatype[] arrayName;

E.g.
char vowels[];

In the above example, we have declared a char array and have named it vowels. Right now, it does not contain
any elements and it is set to null. To actually assign values to the elements of this array, do the following:

vowels = new char[5];

The above statement causes the vowels array to hold 5 elements. new is a java keyword and here it allocates
memory to the array. Now, to assign values to each elements, do the following:

vowels[0] = 'a'; // The first element has the index 0;


vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';

In an array, arrayName.length gives the total number of elements in that array. In our example,
vowels.length will yield 5.

Here is a program to demonstrate one dimensional arrays:

/* A one dimensional array example */

class CharArray
{
public static void main(String args[])
{
char vowels[];
vowels = new char[5];
vowels[0] = 'a'; // The first element has the index 0;
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';

System.out.println(vowels[0]);
System.out.println(vowels[1]);
System.out.println(vowels[2]);
System.out.println(vowels[3]);
System.out.println(vowels[4]);

System.out.println("No of elements in vowels array = " + vowels.length);


}
}

The output of the above program will be:

a
e
i
o
u
No of elements in vowels array = 5

Multi-dimensional Array:- Multi-dimensional arrays are actually arrays of arrays, i.e. an array whose elements are
themselves an array. For example, a 2-dimensional array is the one whose elements are themselves a 1-
dimensional array. A 2-dimensional array can be visualized in a row-column style and is used to represent data in
rows and columns.

/* A 2-dimensional array example */


class TwoDimensionalArray
{
public static void main(String args[])
{
int rows = 3;
int cols = 3;

int arr[][] = new int[rows][cols];

arr[0][0] = 1; /* Element at row 1 column 1*/


arr[0][1] = 2; /* Element at row 1 column 2*/
arr[0][2] = 3; /* Element at row 1 column 3*/
arr[1][0] = 4; /* Element at row 2 column 1*/
arr[1][1] = 5; /* Element at row 2 column 2*/
arr[1][2] = 6; /* Element at row 2 column 3*/

arr[2][0] = 7; /* Element at row 3 column 1*/


arr[2][1] = 8; /* Element at row 3 column 2*/
arr[2][2] = 9; /* Element at row 3 column 3*/

System.out.println(arr[0][0] + " " + arr[0][1] + " " + arr[0][2]); //print the 1st
//row
System.out.println(arr[1][0] + " " + arr[1][1] + " " + arr[1][2]); //print the
//2nd row
System.out.println(arr[2][0] + " " + arr[2][1] + " " + arr[2][2]); //print the 3rd
//row
}
}

The output of the above program will be:


1 2 3
4 5 6
7 8 9

Note that the above program has allocated memory for both the dimensions automatically when the array was
declared. It is also possible to allocate memory manually. This is particularly helpful if you want to create a 2-
dimensional irregular array. A 2-dimensional irregular array is the one whose elements are 1-dimensional arrays of
different lengths. The program below explains 2-dimensional irregular array and manual memory allocation. It
creates a 2-dimensional array whose first element is 1-dimensional array of length 1, second element is a 1-
dimensional array of length 2 and the third element is a 1-dimensional array of length 3.

/*A program to demonstrate 2-dimensional irregular array*/


class TwoDimensionalIrregularArray
{
public static void main(String args[])
{
/*Create an array of 3 rows.*/
int arr[][] = new int[3][];

/*The first element is a 1-dimensional array whose length is 1.


Memory is manually allocated for it*/
arr[0] = new int[1];

/*The second element is a 1-dimensional array whose length is 2.


Memory is manually allocated for it.*/
arr[1] = new int[2];
/*The third element is a 1-dimensional array whose length is 3.
Memory is manually allocated for it.*/
arr[2] = new int[3];

/*Assign values to the first row this array*/

arr[0][0] = 1;

/*Assign values to the second row this array*/


arr[1][0] = 2;
arr[1][1] = 3;

/*Assign values to the third row this array*/


arr[2][0] = 4;
arr[2][1] = 5;
arr[2][2] = 6;

/*Now print the values*/

System.out.println(arr[0][0]);
System.out.println(arr[1][0] + " " + arr[1][1]);
System.out.println(arr[2][0] + " " + arr[2][1] + " " + arr[2][2]);
}
}

The output of the above program will be:

1
2 3
4 5 6

In a similar way, a 3-dimensional array is the one whose elements are 2-dimensional arrays. The declaration
statement below creates a 3-dimensional array of 2 elements which are 2-dimensional arrays with 3 rows and 4
columns.

int arr3D [][][] = new int[2][3][4];

A 3-dimensional array can be visualized as the pages of a book. The pages are the elements of the array and each
page is a 2-dimensional array consisting of rows and columns.

An array can be initialized with values at the time of the declaration of the array. The following piece of code
demonstrates such technique of initialization of a 1-dimensional array:

char charArr[] = { 'a', 'e', 'i', 'o', 'u'};

Similarly, to initialize a 2-dimensional array:

int intArr = {

{1,2,3,4,5},

{11,22,33,44,55},

{111,222,333,444,555}

Now an assignment for the students: - Write a program to create a 3-dimensional array of data type int whose
elements are 2-dimensional arrays of 3 rows and 4 columns and assign values to it.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy