Arrays in java .Types of arrays. operations on arrays. advantages and features of array in java.
Array in java:
Array:
An array is a collection of similar data types. Array is a container object that hold values of homogeneous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.
Array is a collection of similar type of elements which has contiguous memory location.
Array Index: The location of an element in an array has an index, which identifies the element. Array index starts from 0.
Array Element: Items stored in an array is called an element. The elements can be accessed via its index.
Array Length: The length of an array is defined based on the number of elements an array can store. In the above example, array length is 6 which means that it can store 6 elements.
Array starts from zero index and goes to n-1 where n is length of the array.
Advantages of Array:
1. Code Optimization : It makes the code optimized, we can retrieve or sort the data efficiently.
2. Random access : We can get any data located at an index position.
Disadvantages of Array:
Limited size :In java array size is limited.
It is restricted to extend the size of array.
Features of Array :
1. It is always indexed. Index begins from 0.
2. It is a collection of similar data types.
3. It occupies a contiguous memory location.
4. It allows to access elements randomly.
Operations on Arrays :
There are various operations that can be performed on arrays.
Traverse − Print all the elements in the array one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element in the array using the given index or the value.
Update − Updates an element at the given index.
Types of Array in java :
There are two types of array.
1. Single Dimensional Array
2. Multidimensional Array
1. Single Dimensional Array :
A one-dimensional array is also called a single dimensional array where each element is represented by a single subscript . The elements will be accessed in sequential order. This type of array will be accessed by the subscript of either a column or row index.
Declaration of single dimension Array:
Syntax :
datatype[] arrayName;
or
datatype arrayName[];
Initialization of Array :
Initialization is a process of allocating memory to an array. At the time of initialization, we specify the size of array to reserve memory area.
Syntax :
array_name = new datatype[size];
array_name[index]= value;
new operator is used to initialize an array.
Example :
int a[]=new int[5];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
We can combine both declaration and initialization in a single statement.
Datatype[] arrayName = new datatype[size];
we can initialize arrays during declaration.
Example :
//declare and initialize and array
int[ ] age = {12, 4, 5, 2, 5};
Accessing Elements of an Array :
We can access the element of an array using the index number. Here is the syntax for accessing elements of an array,
// access array elements array[index]
Syntax :
array_name[index];
Example :
System.out.println(a[1]);
Single dimension array examples:
here is the list of programs that uses array concept.
1. Java program to accept N array Elements and calculate Sum and Average of array elements.
program:
import java.util.Scanner;
public class Array_Sum
{
public static void main(String[] args)
{
int n, sum = 0;
float avg;
Scanner sc= new Scanner(System.in); System.out.print("Enter no. of elements you want in array:");
n = sc.nextInt();
int a[] = new int[n];
//int a[ ]={10,50,20,40,30};
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
sum = sum + a[i];
}
System.out.println("Sum is:"+sum);
avg=(float)sum/n;
System.out.println("Average is:"+avg);
}
}
Output:
Enter no. of elements you want in array :
5
Enter all the elements:
1
2
3
4
5
Sum is:15
Average is:3.0
2. Program to sort an array elements using selection sorting technique.
program:
import java.util.*;
public class Array_Sort
{
public static void main(String args[])
{
int n,i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter how many elements u want :");
n = sc.nextInt();
int a[] = new int[n];
System.out.print("Enter elements of an Array:" );
for( i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
// selection sort
for( i=0;i<n-1;i++)
{
for( j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
int t=a[i];
a[i] = a[j];
a[j] = t;
}
}
}
System.out.println("sorted elements of an Array:" );
for( i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
Output :
Enter how many elements u want :
4
Enter elements of an Array:
3
6
4
2
sorted elements of an Array:
2
3
4
6
3. Write a Java program to sort the elements of an array using bubble sort.
Program:
import java.util.*;
public class Array_Sort
{
public static void main(String args[])
{
int n, i, j, temp;
int a[] = new int[50];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Elements u want: ");
n = sc.nextInt();
System.out.println("Enter the Elements of array: ");
for(i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
// Bubble Sort
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("Array Sorted Successfully..!!\n");
System.out.print("Sorted Elements in Ascending Order : \n");
for(i=0; i<n; i++)
{
System.out.println(a[i]);
}
}
}
Output :
Enter Number of Elements u want:
5
Enter the Elements of array:
10
30
40
50
20
Array Sorted Successfully..!!
Sorted Elements in Ascending Order :
10
20
30
40
50
4.Write a Java program to search an element using binary search using array.
Program:
import java.util.*;
public class Binary_Search
{
public static void main(String args[])
{
int n, i, search, first, last, middle;
int a[] = new int[50];
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number of Elements u want: ");
n = sc.nextInt();
System.out.println("Note : please,Enter elements of an array either asending or desending order.");
System.out.println("Enter " +n+ " Elements :");
for(i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
System.out.println("Enter a key value to Search:");
search = sc.nextInt();
//Binary search
first = 0;
last = n-1;
middle = (first+last)/2;
while(first <= last)
{
if(a[middle] < search)
{
first = middle+1;
}
else if(a[middle] == search)
{
System.out.println(search+ " Found at Location " +(middle+1));
break;
}
else
{
last = middle - 1;
}
middle = (first+last)/2;
}
if(first > last)
{
System.out.println("Not Found..!! " +search+ " is not Present in the List.");
}
}
}
Output :
Enter the Number of Elements u want:
6
Note : please,Enter elements of an array either asending or desending order
Enter 6 Elements :
1
2
3
5
10
15
Enter a key value to Search:
5
5 Found at Location 4
Or
Enter the Number of Elements u want:
5
Note : please,Enter elements of an array either asending or desending order
Enter 5 Elements :
1
2
3
5
15
Enter a key value to Search:
7
Not Found..!! 7 is not Present in the List.
5. Write a Java program to search an element using linear(sequential) search using array.
program:
import java.util.*;
public class Linear_Search
{
public static void main(String args[])
{
int n,i,key;
int a[] = new int[50];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Total Number of Elements : ");
n = sc.nextInt();
System.out.println("Enter " +n+ " Elements : ");
for(i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
System.out.println("Enter a keyValue to Search :");
key= sc.nextInt();
//searching
for (i = 0; i< n; i++)
{
if (a[i] == key)
{
System.out.println(key+" is present at location "+(i+1));
break;
}
}
if (i== n)
System.out.println(key+ " doesn't exist in array.");
}
}
Output :
Enter Total Number of Elements :
5
Enter 5 Elements :
10
20
40
60
30
Enter a keyValue to Search :
30
30 is present at location 5
Or
Enter Total Number of Elements :
5
Enter 5 Elements :
10
40
5
6
3
Enter a keyValue to Search:
7
7 doesn't exist in array.
2. Multi-Dimensional Array :
The number of dimensions specified is more than one, then it is called as a multi-dimensional array. Multidimensional arrays include 2D arrays and 3D arrays
A two-dimensional array will be accessed by using the subscript of row and column index. each element is represented by two subscripts.
Multidimensional arrays are arrays of arrays.
Declaration of Array:
Syntax:
data_type[dimension 1][dimension 2][]…[dimension n] array_name= new data_type[size 1][size 2]…[size n];
Or
data_type array_name[][] =new data_type[rows][columns];
Example:
int a[][]=new int[2][2];
Initialization of Array :
Syntax :
array_name[row][column]=value;
Example :
arr[0][0]=1;
a[0][1]=2;
a[1][0]=3;
a[1][1]=4;
Multidimensional array examples:
here is the list of programs that uses multidimensional array concept.
1. write a java program to perform addition and substraction of two matrices using arrays.
Program:
import java.util.*;
public class matrixopera
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the order of the matrix :");
int r=sc.nextInt();
int c=sc.nextInt();
int i, j, k;
int a[ ][ ]=new int[r][c];
int b[ ][ ]=new int[r][c];
int res[ ][ ]=new int[r][c];
System.out.println("Enter the elements for matrixA:");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter the elements for matrix B:");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
b[i][j]=sc.nextInt();
}
}
//matrix addition
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
res[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Addition of matrix :");
System.out.println("Resultant matrix is:");
for(i=0;i<r;i++)
{ System.out.println();
for(j=0;j<c;j++)
{
System.out.print(res[i][j]+"\t");
}
}
//matrix substraction
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
res[i][j]=a[i][j]-b[i][j];
}
}
System.out.println("\nsubstraction of matrix :");
System.out.println("Resultant matrix is:");
for(i=0;i<r;i++)
{ System.out.println();
for(j=0;j<c;j++)
{
System.out.print(res[i][j]+"\t");
}
}
}
}
Output:
Enter the order of the matrix :
2 2
Enter the elements for matrixA:
1 2
3 4
Enter the elements for matrix B:
5 6
7 8
Addition of matrix:
Resultant matrix is:
6 8
10 12
substraction of matrix :
Resultant matrix is:
-4 -4
-4 -4
Note:
The two matrix involving in addition and substraction operation must have same number of rows and columns.(order of matrices must be same).
2.Write a java program to perform multiplication(product) of two matrice using array(2-D).
Program:
import java.util.*;
public class matrixmul
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row and columns for matrix A:");
int r1=sc.nextInt();
int c1=sc.nextInt();
System.out.println("Enter the row and columns for matrix B:");
int r2=sc.nextInt();
int c2=sc.nextInt();
int i, j, k;
int a[ ][ ]=new int[r1][c1];
int b[ ][ ]=new int[r2][c2];
int c[ ][ ]=new int[r1][c2];
if(c1!=r2)
System.out.println("Matrix multiplication is not possible");
else
{
System.out.println("Enter the elements for matrixA:");
for(i=0;i<r1;i++)
{ for(j=0;j<c1;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter the elements for matrix B:");
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
{
b[i][j]=sc.nextInt();
}
}
//matrix multiplication
for(i=0;i<r1;i++)
{ for(j=0;j<c2;j++)
{ c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("Resultant matrix is:");
for(i=0;i<r1;i++)
{ System.out.println();
for(j=0;j<c2;j++)
{
System.out.print(c[i][j]+"\t");
}
}
}
}
}
Output :
Enter the row and columns for matrix A:
2 2
Enter the row and columns for matrix B:
2 2
Enter the elements for matrixA:
1 2
3 4
Enter the elements for matrix B:
5 6
7 8
Resultant matrix is:
19 22
43 50
Or
Enter the row and columns for matrix A:
2 3
Enter the row and columns for matrix B:
2 2
Matrix multiplication is not possible.
Description:If the order of matrix A is r1 x c1 and of matrix B is r2 x c2 (number of
columns of A = number of rows of B = c1=r2), then the order of matrix C is r1 x c2,
where C = A x B otherwise matrix multiplication is not possible. First accept number of
rows and columns of matrix A into r1, c1 then accept number of rows and columns of
matrix B into r2, c2. If c1 is not equal to r2 then display a message matrix multiplication
is not available otherwise accept two matrices into two arrays and perform the
multiplication operation and store the result in another array and display the same as result.
3.Write a java program to find the transpose of given matrix using arrays.
Program:
import java.util.*;
public class matrixtrans
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the rows and columns of the matrix :");
int r=sc.nextInt();
int c=sc.nextInt();
int i, j;
int a[ ][ ]=new int[r][c];
System.out.println("Enter the elements for matrix:");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Before transose of matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("After transpose of matrix:");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++){
System.out.print(a[j][i]+ "\t");
}
System.out.println();
}
}
}
Output :
Enter the rows and columns of the matrix :
2
2
Enter the elements for matrix:
1
2
3
4
Before transose of matrix:
1 2
3 4
After transpose of matrix:
1 3
2 4
4. write a java program to find sum of all elements in a given matrix using array.
program:
import java.util.*;
public class matrixtrans
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the rows and columns of the matrix :");
int r=sc.nextInt();
int c=sc.nextInt();
int i, j,sum=0;
int a[ ][ ]=new int[r][c];
System.out.println("Enter the elements for matrix:");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("elements of matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
sum+=a[i][j];
}
System.out.println();
}
System.out.println("sum of all elements in matrix:"+sum);
}
}
Output:
Enter the rows and columns of the matrix :
2
2
Enter the elements for matrix:
1
2
3
4
elements of matrix:
1 2
3 4
sum of all elements in matrix:10
Comments
Post a Comment