What is an Array?

ARRAY: An Array is a group of contiguous/related data items that share a common name. A particular value is indicated by writing a number called index number /subscript in brackets after the array name. Eg: Salary [20] Advantages:  Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.  Random access: We can get any data located at an index position. Disadvantages:  Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically. CREATING AN ARRAY: Creation of an array involves three steps: 1. Declare the array 2. Create memory location 3. Put values into the memory location DECLARATION OF ARRAYS: Arrays in Java can be declared in Two forms: type arrayname [ ]; Form 1 type [] arrayname; Form 2 Eg: int employee[] float [] number we do not enter the size of the arrays in the declaration. CREATION OF ARRAYS: After declaring an array, we need to create it in the memory. Java allows us to create arrays using new operator only, arrayname = new type[size]; Eg: salary =new int[10]; INITIALIZATION OF ARRAY: The final step is to put values into the array created. This process is known as Initialization. This is done using the array subscripts Eg: mark[0] = 78; mark [1] = 65; ………… ………… ………… mark [5] = 40; Note that Java creates arrays starting with a subscript of 0 and ends with a value one less than the size specified. Java protects arrays from overruns and underruns. Trying to access an array beyond its boundaries will generate an error message. We also initialize arrays automatically in the same way as the ordinary variables when they are declared, type arrayname [] = {list of values}; The array initialize is a list of values separated by commas and surrounded by curly braces. Note that no size is given. The Compiler allocates enough space for all the elements specified in the list. Eg: int number [] = {56, 87, 32, 42, 95, 15}; ARRAY LENGTH: All arrays store the allocated size in a variable named length. We can access the length of the array a using a.length. This information will be useful in the manipulation of arrays when their sizes are not known. Class Example { public static void main(String args []){ // To find the length of an array int[] arr = new int[5]; // 'length' is a function in Java which gives you the size of an array System.out.println(arr.length); } } TYPES OF ARRAY : There are two types of array. I. One- Dimensional Array II. Two-Dimensional Array One- Dimensional Array : As list of items can be given one variable name using only one subscript and such a variable is called Single-Subscripted Variable/ a one-Dimensional array. Subscripted variables can be represented as X[0], X[1], X[2], ……….X[n]. Eg: if we want to represent a set of five numbers by an array variable, then we may create the variable number as follows int number [] = new int[5]; and the computer reserves five storage locations numbers={45,98,65,12,35}; number [0] number[1] number [2] number[3] number[4] The values to the array elements can be assigned as number [0] = 45; number [1] = 98; number [2] = 65; number [3] = 12; number [4] = 35; Eg: Class One-D { public static void main(String args []) { int [] a = new int [5]; a [0] = 23; a [1] = 98; a [2] = 36; a [3] = 74; a [4] = 18; System.out.println(a [0]); System.out.println(a [1]); System.out.println(a [2]); System.out.println(a [3]); System.out.println(a [4]); } } Multidimensional Array : In such case, data is stored in row and column based index (also known as matrix form). Syntax to Declare Multidimensional Array: type arrayname [] []; type [] [] arrayname; Example to instantiate Multidimensional Array: table =new int[3][3]; Eg: Class Two-D { public static void main (String args[]){ int [][] b= new int[2] [3]; b [0] [0]=101; b [0] [1]=202; b [0] [2]=303; b [1] [0]=404; b [1] [1]=505; b [1] [2]=606; System.out.println(b [0] [0]); System.out.println(b [0] [1]); System.out.println(b [0] [2]); System.out.println(b [1] [0]); System.out.println(b [1] [1]); System.out.println(b [1] [2]); } } VARIABLE SIZE ARRAYS: Java treats multidimensional array as “arrays of arrays”. It is possible to declare a two-dimensional array as follows: int X [] [] = new int [3] []; X [0] = new int [2]; X [1] = new int [4]; X [2] = new int [3]; .
SEE ALL Add a note
YOU
Add your Comment
 

Search Courses

About

Ajay Skills Academy is an initiative by
Markose m-Learning Wireless (MMW) Pvt Ltd.

0 (91) 9443792391
info@ajayskillsacademy.org

Our Offices @

North America

Europe

Asia

top
Ajay Skills Academy - An Initiative by Markose M-Learning (MMW)(P)Ltd.
X