Arrays

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SUBJECT: Java TOPIC: Arrays

WRITTEN BY: SRINIVAS YADLAPATY, MTech,(PHD) in


Computer Science Engineering
---------------------------------------------------------------------------------
Arrays:
An array represents a group of elements of same data type.
Advantage of arrays:
The main advantage of arrays is to represent and handle groups of
elements.
Types of arrays:
There are 3 types of arrays.
1) One dimensional array (Single dimensional array-1D array)
2) Two dimensional array (2D array)
3) Multidimensional array (3D array, 4D array etc)
1D:
A 1D array represents a single row or a single column of elements.
Example: Marks obtained by a student in 5 different subjects.
Creating a 1D array:
Initializing a 1D array:
We can create a 1D array by declaring it and directly initializing it.
int marks[] = {70,67,89,56,90};
where as,
‘70,67,89,56,90’ are the elements of the array.
‘marks’ is the name of the array.
‘int’ is the type(datatype) of the array.
‘size’ of the array is 5 in the above example.
Indexing always starts from 0 and
array[0]=70
array[1]=67
array[2]=89
array[3]=56
array[4]=90
The last positioned element is present in size-1=>5-1=4th index.
JVM will allot 5 contiguous blocks of memory and store it.
marks[i]
i=> index of array
i is an integer number that represents the position of array.
70 67 89 56 90
marks[0] marks[1] marks[2] marks[3] marks[4]

1D has only 1 index


float x[] = {1.25f,26.34f,55.55f};
char ch[] = {‘a’,’b’,’x’,’n’};
String name[] = {“Ram”,”Raj”,”Ravi”,”Rohan”};
Declaring a 1D array:
We can create a 1D array dynamically using new operator.
int marks[] = new int[5] ;
marks[0] = 55;
marks[1] = 67;
marks[2] = 77;
double salary[] = new double[100];
String name[] = new String[50];
Program:
// Application on finding total and average obtained by a student in n
// subjects.
import java.io.*;
class Student
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("How many subjects:");
int n=Integer.parseInt(br.readLine());
int marks[]=new int[n];
int total=0;
for(int i=0;i<n;i++)
{
System.out.println("Enter marks:");
marks[i]=Integer.parseInt(br.readLine());
total=total+marks[i];
}
System.out.println("Total marks obtained="+total);
float average=(float)total/n;
System.out.println("Average marks obtained="+average);
}
}
Output:
How many subjects:
3
Enter marks:
89
Enter marks:
90
Enter marks:
99
Total marks obtained=278
Average marks obtained=92.666664
2D:
A 2D array represents several rows and columns of elements.
Creating a 2D array:
Initializing a 2D array:
We can declare and directly initialize a 2D array.
int marks[][] = {{56,89,45,90,89},{67,78,99,67,68},
{55,98,87,78,90}};

ro 56 89 45 90 89
r1 67 78 99 67 68
r2 55 98 87 78 90
c0 c1 c2 c3 c4
marks[0][1] = 89
marks[i][j]
i and j are indices. 2D has 2 indices.
A 2D is a combination of several 1D arrays.
Similarly a 3D is a combination of several 2D arrays.
Similarly a 4D is a combination of several 3D arrays.

float x[][] = {{1.1f,2.4f,5.5f,6.3f},


{2.3f,4.6f,5.7f,6.3f},
{3.75f,5.55f,6.1f,4.89f}};
Declaring a 2D array:
We can create a 2D array using new operator on dynamic memory.
int marks[][] = new int[5][6];
char ch[][] = new char[10[20];
Program:
//Program on 2D array display.
class Matrix
{
public static void main(String args[])
{
float x[][] = {{1.1f,2.4f,5.5f,6.3f},
{2.3f,4.6f,5.7f,6.3f},
{3.75f,5.55f,6.1f,4.89f}};
// Read and display in matrix form.
System.out.println("In matrix form:");
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
{
System.out.print(x[i][j]+"\t");
}
System.out.println();
}
}
}
Output:
In matrix form:
1.1 2.4 5.5 6.3
2.3 4.6 5.7 6.3
3.75 5.55 6.1 4.89
Important interview questions:
1. On which memory arrays are created in Java?
A. Arrays are created on dynamic memory by JVM. There is no question
of static memory in Java everything (variable, array, object, etc.) is
created on dynamic memory only.
2. What is an array in Java?
A. An array is a finite and ordered collection of homogeneous data
elements. It is finite because it contains a limited number of
elements. It is ordered because all the elements are stored one by
one in a contiguous location of computer memory (heap) in a linear
fashion. It is homogeneous because all elements of an array are of
the same data type only. We can store either primitive types or
object references into it.
3. What are the types of an array?
A. Arrays are generally categorized into two parts as described below:
o Single Dimensional Array
o Multi-Dimensional Array (2D and 3D arrays)
4. Is it possible to declare array size as negative?
A. No, it is not possible to declare array size as negative. Still, if we
declare the negative size, there will be no compile-time error. But we
get the NegativeArraySizeException at run-time.
5. What is the difference between int array[] and int[] array?
A. There is no difference between array[] and []array. Both array[] and
[]array are the ways to declare an array. The only difference between
them is that if we are declaring more than one array in a line, we
should use prefix []. If we are declaring a single array in a line, we
should use postfix []. For example, consider the following declaration:
int array1[], array2; //array1[] is an array while array2 is just a varia
ble of type int
int[] arr1, arr2; //both arr1 and arr2 are arrays of int type
6. Which operations can be performed on an array?
A. On an array, we can perform the searching, sorting, traversal,
deletion, and insertion operation.
7. What is the default value of the array?
A. When we create a new array, it always initialized with the default
values. The default values of the array are:
o If an array is of byte, short, int, and long type, the default value is 0.
o If an array is of float and double type, the default value is 0.
o If an array is of Boolean type, the default value is false.
o If an array is of an Object type, the default value is null.

You might also like