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

OOP CS3391 UNIT-1[Lecture Note-12]

An array in Java is a collection of similar type elements stored in contiguous memory locations, allowing for optimized code and random access. There are two types of arrays: one-dimensional and multidimensional, with specific methods for declaration, instantiation, and initialization. While arrays provide efficient data handling, they have a fixed size limitation and cannot grow dynamically at runtime.

Uploaded by

RajeeshKumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

OOP CS3391 UNIT-1[Lecture Note-12]

An array in Java is a collection of similar type elements stored in contiguous memory locations, allowing for optimized code and random access. There are two types of arrays: one-dimensional and multidimensional, with specific methods for declaration, instantiation, and initialization. While arrays provide efficient data handling, they have a fixed size limitation and cannot grow dynamically at runtime.

Uploaded by

RajeeshKumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ARRAYS

Definition:
 An array is a collection of similar type of elements which has contiguous memory
location.
 Java array is an object which contains elements of a similar data type.
 The elements of an array are stored in a contiguous memory location.
 It is a data structure where we store similar elements.
 Array in Java is index-based, the first element of the array is stored at the 0th index,
2nd element is stored on 1st index and so on.
Advantage of Array:
• Code Optimization: It makes the code optimized; we can retrieve or sort the data easily.
• Random access: We can get any data located at any index position.

Disadvantage of Array:
Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at
runtime.

Types of Array:
There are two types of array.
1. One-Dimensional Arrays
2. Multidimensional Arrays
1. One-Dimensional Array:
One-dimensional array is an array in which the elements are stored in one variable name by
using only one subscript.

Creating an array:
Three steps to create an array:
1. Declaration of the array
2. Instantiation of the array
3. Initialization of arrays

1. Declaration of the array:


Declaration of array means the specification of array variable as
data_type array_name;

Syntax to Declare an Array in java:


Example:
dataType[] arrayName; (or) int[] arr; (or)
dataType []arrayName; (or) Int []arr (or)
int arr[];
dataType arrayName[];
2. Instantiation of the array:
Allocating memory spaces for the declared array in memory (RAM) is called as Instantiation
of an array.
Syntax:
array_name = new datatype[size];
Example: arr = new int[10];

3. Initialization of arrays:
Storing the values in the array element is called as Initialization of arrays.

Syntax to initialize values to array element:


array_name[index value] = constant or value;
Example:
arr[0] = 20;
arr[1] = 30;
Java has shorthand to create an array object and supply initial values at the same time
when it is created.

Syntax:

dataType array_name[] = {list of values};


(or)
dataType array_name[] = array_variable;

Example 1:
int regno[] = {101,102,103,104,105,106};
int reg[] = regno;
ARRAY LENGTH:
The keyword length can identify the length of array in Java. To find the number of
elements of an array, use array_name.length

Example1:
int regno[10];
len=regno.length;
System.out.println(len);

Example 2:
for(int i=0; i<reno.length; i++)
{
System.out.println(regno[i]);
}
class Array1
{
public static void main(String[] args)
{
String month_days[];
month_days=new String[12];
month_days[0]="Jan";
month_days[1]="Feb";
month_days[2]="Mar";
month_days[3]="Apr";
month_days[4]="May";
month_days[5]="Jun";
month_days[6]="July";
month_days[7]="Aug";
month_days[8]="Sept";
month_days[9]="Oct";
month_days[10]="Nuv";
month_days[11]="Dec";
System.out.println("the fifth month is "+month_days[4]);
Output:
}
} the fifth month is May
Example 2: Finding sum of the array elements and maximum from the array:

public class TestArray max = myList[0];


{ for (int i = 1; i < myList.length; i++)
public static void main(String[] args) {
{ if (myList[i] > max)
int myList[] = {10, 20, 30, 40}; max = myList[i];
int total=0; }
int max; System.out.println("Max is " + max);
for (int i = 0; i < myList.length; i++) }
{ }
total += myList[i];
}
System.out.println("Total is " + total); Output:
Total is 100
Max is 40
2. Multidimensional Arrays:

Multidimensional arrays are arrays of arrays. It is an array which uses more than one
index to access array elements. In multidimensional arrays, data is stored in row and
column based index (also known as matrix form).
Uses of Multidimensional Arrays:
 Used for table
 Used for more complex arrangements

Syntax to Declare Multidimensional Array in java:

1. dataType[][] array_name; (or)


2. dataType [][] array_name; (or)
3. dataType array_name[][];

Example:
int[][] arr;
int arr[][];
int [][]arr;
Example to instantiate Multidimensional Array in java:
int[][] arr=new int[3][3];

Example to initialize Multidimensional Array in java:


arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Examples to declare, instantiate, initialize and print the 2Dimensional array:

Example-1 class TwoDarray


{
public static void main(String args[])
{
int array1[][]=new int[4][5];
int i,j,k=0;
System.out.println("-------Array 1------- " );
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
Output:
array1[i][j]=k;
k++; -----Array 1-----
System.out.print(array1[i][j]+ " ");
0 1 2 3 4
}
5 6 7 8 9
System.out.println();
}}} 10 11 12 13 14
15 16 17 18 19
Examples to declare, instantiate, initialize and print the 2Dimensional array:

Example-2
class TwoDarray2
{
public static void main(String args[])
{
int array2[][] = {{1,2,3},{2,4,5},{4,4,5}}; // declaring and initializing 2D array
int i, j, k = 0;
System.out.println("-----Array 2----- ");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++) Output:
{
System.out.print(array2[i][j] + " "); -----Array 2-----
} 1 2 3
System.out.println(); 2 4 5
} 4 4 5
}
}

You might also like