“Arrays”

mahnoor Solangi
Nov 2 · 2 min read

In second lab we have learned about the arrays and its methods.

So,

An Array is a data structure that contains a group of elements. Typically these these elements are all of the same data types , such as an integer or string. Array are commonly used to organized data , So that related set of value can be easily sorted and searched.

Declaring Array Variables:

An Array declaration requires the base type the type that each element of the Array will be eg: (char or int) the name of the Array and the size of the Array in square braces. type name [size].

Creating Array:

SYNTAX:

type var-name[]; or type[] var-name;

Initializing Array:

SYNTAX:

var-name new type[size];

Allocating of Array:

SYNTAX:

int intArray[]; // Declaring Array.

int Array = new int[20]; // Allocating memory to array.

or

int [] int Array = new int[20]; // combining both statements in one.

  1. The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types).Refer Default Array values in Java
  2. Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated.

Arrays of Object:

An array of objects is created just like an array of primitive type data items in the following way.

Student [] arr = new Student[7]; // Student is a user defined class.

The studentArray contains seven memory spaces each of size of student class in which the address of seven Student objects can be stored.The Student objects have to be instantiated using the constructor of the Student class and their references should be assigned to the array elements in the following way.

Multidimensional Array:

Multidimensional arrays are Arrays of arrays with each element of the array holding the reference of other array. These are also known as Jagged Arrays. A multidimensional array is created by appending one set of square brackets ([]) per dimension. Examples:

int [] [] intArray = new int [10] [20]; // a 2D array or matrix.

int [] [] [] intArray = new int [10][20][10]; // a 3D array.

class MultiDimensional

{

public static void main (String []args)

{

// declaring and initializing 2D array.

int arr[][] = { {2,7,9},{3,6,1},{7,4,2} };

// printing 2D array.

for(int i=0; i< 3; i++)

{

for(int j=0; j < 3 ; j++)

{

System.out.print(arr[i][j] + “ “);

}

System.out.println();

}

}

}

THE END :)

    mahnoor Solangi

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade