Array
An array is a data structure that stores a collection of elements (values or
variables), usually of the same data type, in a contiguous block of memory .
Each element in an array can be accessed using an index (or subscript).
We have already learnt that the index starts with 0.
Advantages of array:
➢ Stores many values together
➢ Easy access using index
➢ Better code organization
➢ Memory efficiency
➢ Useful in algorithms
Disadvantages of array:
➢ Fixed size
➢ Same data type only
➢ Wastage of memory
➢ No Built-in Methods
➢ Difficult to insert/delete
Types of arrays:
There are two types of arrays:
i. Single dimensional array
ii. Multidimensional array
Following are the three tasks to manipulate an array:
- Declaration of the array
- Allocate memory for it
- Loading the values for it
Single dimensional array (1D array):
- Declaration of the array
Syntax:
< data types > < array name> [];
OR
< data types > [] < array name>;
Example:
int arr []; OR int [] arr;
- Allocate memory for it
Syntax:
<array name> = new <data types> [<size>];
Example:
arr=new int [10];
- Loading the values into array
for(int i=0;i<10;i++)
{
arr [i]=[Link]();
}
Define and allocate memory together:
Syntax:
<data types> <array name> [] = new <data types> [<size>];
Example:
int arr [] =new int [10];
Another way of declaring and initialization array:
int arr [] = {10, 20, 30, 40, 50};
arr [0] = 10
arr [1] = 20
arr [2] = 30
arr [3] = 40
arr [4] = 50
Write a program to input the integer array of size 10 and display it.
import [Link].*;
class ArrayDemo
{
int arr[]=new int[10];
public void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void display()
{
[Link]("Array elements are : ");
for(int i=0;i<[Link];i++)
{
[Link](arr[i]);
}
}
public static void main(String [] args)
{
ArrayDemo ad=new ArrayDemo();
[Link]();
[Link]();
}
}
Write a program to input the integer array of size 10 and find the sum of the
elements and display it.
import [Link].*;
class Sum
{
int arr[]=new int[10];
int sum=0;
public void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void getSum()
{
for(int i=0;i<[Link];i++)
{
sum=sum+arr[i];
}
}
public void display()
{
[Link]("Sum : "+ sum);
}
public static void main(String [] args)
{
Sum s=new Sum();
[Link]();
[Link]();
[Link]();
}
}
Write a program to find the maximum elements in the integer array of
size 10 and display it.
import [Link].*;
class Maximum
{
int arr[]=new int[10];
int max;
public void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void getMax()
{
for(int i=0;i<[Link];i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
}
public void display()
{
[Link]("Maximum : "+ max);
}
public static void main(String [] args)
{
Maximum m=new Maximum();
[Link]();
[Link]();
[Link]();
}
}
Write a program to input the integer array of size 10 and performs linear
search on it and display its position if element present and if element is
absent then, display “Element is not present”.
import [Link].*;
class LinearSearch
{
Scanner sc=new Scanner([Link]);
int arr[]=new int[10];
int search;
int flag =-1;
public void input()
{
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void getSearchElement()
{
[Link]("Enter the elements to be searched : ");
search=[Link]();
}
public void getSearch()
{
for(int i=0;i<[Link];i++)
{
if(arr[i]==search)
{
[Link]("Element is present at the position : " +
((i+1)));
flag++;
break;
}
}
}
public void check()
{
if(flag==-1)
{
[Link]("Element is not present");
}
}
public static void main (String args [] )
{
LinearSearch ls=new LinearSearch();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Write a program to input the integer array of size 10 and perform
binary search on it and display its positions if element present and if
element is absent then, display “Element is not present”.
import [Link].*;
class BinarySearch
{
Scanner sc=new Scanner([Link]);
int arr[]=new int[10];
int search;
int first,last,mid;
int flag=-1;
public void input()
{
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void getSearchElement()
{
[Link]("Enter the elements to be search : ");
search=[Link]();
}
public void getSearch()
{
first=0;
last=[Link]-1;
while(first<=last)
{
mid=(first+last)/2;
if(search<arr[mid])
{
last=mid-1;
}
else if(search>arr[mid])
{
first=mid+1;
}
else
{
[Link]("Elements is present at the position : " +
(mid + 1));
flag++;
break;
}
}
}
public void check()
{
if(flag==-1)
{
[Link]("Element is not present");
}
}
public static void main(String [] args)
{
BinarySearch bs=new BinarySearch();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Write a program to input the integer array of size 10 and sort them in
ascending order using exchange selection sort and display it.
import [Link].*;
class SelectionSort
{
Scanner sc=new Scanner([Link]);
int arr[]=new int[10];
int temp,small,pos;
public void input()
{
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void sort()
{
for(int i=0;i<[Link];i++)
{
small=arr[i];
pos=i;
for(int j=(i+1);j<[Link];j++)
{
if(arr[j]<small)
{
small=arr[j];
pos=j;
}
}
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
}
public void display()
{
[Link]("Sorted array are : ");
for(int i=0;i<[Link];i++)
{
[Link](arr[i]);
}
}
public static void main(String args [])
{
SelectionSort ss=new SelectionSort();
[Link]();
[Link]();
[Link]();
}
}
Write a program to input the integer array of 10 elements and sort them
in descending order using bubble sort and display it.
import [Link].*;
class BubbleSort
{
Scanner sc=new Scanner([Link]);
int arr[]=new int[10];
public void input()
{
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
}
public void sort()
{
int temp;
for(int i=0;i<[Link];i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(arr[j+1]>arr[j])
{
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
}
public void display()
{
[Link]("Sorted array are : ");
for(int i=0;i<[Link];i++)
{
[Link](arr[i]);
}
}
public static void main(String args [])
{
BubbleSort bs=new BubbleSort();
[Link]();
[Link]();
[Link]();
}
}
♦ Few important questions of 1D array:
• Write a program to input an integer array of 10 elements
and display it.
• Write a program to input an integer array of 10 elements
and find the maximum element and display it.
• Write a program to input an integer array of 15 elements
and find the minimum element and display it.
• Write a program to input 10 elements of integer array and
calculate the sum of the integer array and display it.
• Write a program to input an integer array of 10 elements
and perform linear search on the elements of an integer
array.
• Write a program to perform binary search on the elements
of a following integer array: -
{11, 13, 23, 34, 34, 54, 56, 67, 76, 89}
• Write a program to input an integer array of 10 elements
and perform exchange selection sort on the elements of an
integer array.
• Write a program to perform bubble sort on the elements of
a following integer array: -
{71, 63, 73, 48, 34, 94, 36, 67, 56, 89}
• Write a program to input 20 cities names with today
temperatures. Find and display the top 5 hottest cities with
their temperatures.
• Write a program to input 10 student names with there
admission number and perform search of an admission
number and if searched admission number exit, display it
with its student name.
• Write a program to input integer array of 5 elements as
well as input another integer array of 10 elements and
merge (combine) them into a single array.
• Write a program to input and store roll numbers, names
and marks in 3 subjects of n number of students in five
single dimensional arrays and display the remark based
on average marks as given below:
Average marks Remarks
85-100 Excellent
75-84 Distinction
60-74 First Class
40-59 Second Class
Less than 40 Poor
• Write a program to input twenty names in an array.
Arrange these names in descending order of letters,
using the bubble sort technique.
• Write a program to initialize the seven Wonders of the
World along with their locations in two different arrays.
Search for a name of the country input by the user. If
found, display the name of the country along with its
Wonder, otherwise, display "Sorry not found!".
Seven Wonders:
CHICHEN ITZA, CHRIST THE REDEEMER, TAJ
MAHAL, GREAT WALL OF CHINA, MACHU
PICCHU, PETRA, COLOSSEUM
Locations:
MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN,
ITALY
Examples:
Country name: INDIA
Output: TAJ MAHAL
Country name: USA
Output: Sorry Not found!
• Write a program to input integer elements into an array
of size 20 and perform the following operations:
i) Display largest number from the array.
ii) Display smallest number from the array.
iii) Display sum of all the elements of the array.
• Define a class to accept 10 characters from a user. Using
bubble sort technique arranges them in ascending order.
Display the sorted array and original array.
• Define a class to declare a character array of size ten.
Accept the characters into the array and display the
characters with highest and lowest ASCII (American
Standard Code for Information Interchange) value.
Example:
Input:
'R', 'z', 'q', 'A', 'N', 'p', 'm', 'U', 'Q', 'F'
Output:
Character with highest ASCII value = z
Character with lowest ASCII value = A
• Define a class to declare an array of size twenty of
double datatype, accept the elements into the array and
perform the following:
a. Calculate and print the product of all the elements.
b. Print the square of each element of the array.
• Define a class to declare an array to accept and store ten
words. Display only those words which begin with the
letter 'A' or 'a' and also end with the letter 'A' or 'a'.
Example:
Input: Hari, Anita, Akash, Amrita, Alina, Devi Rishab,
John, Farha, AMITHA
Output:
Anita
Amrita
Alina
AMITHA
• Write a program to accept name and total marks
of N number of students in two single subscripts
array name[] and totalmarks[].
Calculate and print:
i) The average of the total marks obtained by N number
of students.
[average = (sum of total marks of all the students)/N]
ii) Deviation of each student’s total marks with the
average.
[deviation = total marks of a student – average]
Multi-dimensional array (more than 1D array):
Simplest form of multi-dimensional form is 2 D array:
- Declaration of the 2D array
Syntax:
< data types > < array name> [] [];
OR
< data types > [] [] < array name>;
Example:
int arr [][]; OR int [][] arr;
- Allocate memory for it
Syntax:
<array name> = new <data types> [<row size>] [<column size>];
Example:
arr=new int [3][2];
- Loading the values into 2D array
for(int i=0;i<[Link];i++)
{
for(int j=0;j<arr[i].length;j++)
{
arr[i][j]=[Link]();
}
}
Define and allocate memory together:
Syntax:
<data types> <array name> [] = new <data types> [<row size>]
[<column size>];
Example:
int arr [] =new int [3] [2];
Another way of declaring and initialization array:
int arr [] = {{10, 20}, {30, 40}, {50,60}};
arr [0] [0] = 10
arr [0] [1] = 20
arr [1] [0] = 30
arr [1] [1] = 40
arr [2] [0] = 50
arr [2] [1] = 60
Write a program to input the integer array of size 3 × 3 and display it.
import [Link].*;
class Array_2D_Demo
{
int arr[][]=new int[3][3];
public void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the elements of the array : ");
for(int i=0;i<[Link];i++)
{
for(int j=0;j<arr[i].length;j++)
{
arr[i][j]=[Link]();
}
}
}
public void display()
{
[Link]("2 D array is : ");
for(int i=0;i<[Link];i++)
{
for(int j=0;j<arr[i].length;j++)
{
[Link](arr[i][j]+"\t");
}
[Link]();
}
}
public static void main(String [] args)
{
Array_2D_Demo a=new Array_2D_Demo();
[Link]();
[Link]();
}
}
♦ Few important questions of 2D array:
• Write a program to input the integer array of size 3 × 3 and display it.
• Write a program to input the integer array of size 4 × 4 and find the
sum of all the elements of the array and display it.
• Write a program to input the integer array of size 3 × 3 and find the
sum of the left diagonal of the array and display it.
• Write a program to input the integer array of size 4 × 4 and find the
sum of the right diagonal of the array and display it .
• Write a program to input the integer array of size 4 × 4 and find the
sum of border elements of the array and display it.
• Write a program to input the integer array of size 3 × 3 and find the
sum of non-border elements of the array and display it.
• Write a program to input the integer array of size 5 × 5 and find the
sum of corner elements of the array and display it.
• Write a program to input the integer array of size 4 × 4 and find the
sum of non-corner elements of the array and display it .
• Write a program to input the integer array of size 4 × 4 and find the
sum of each row of the array and display it.
• Write a program to input the integer array of size 4 × 4 and find the
sum of each row of the array and display it.
• Write a program to input the integer array of size 4 × 4 and find the
sum of each column of the array and display it.
• Write a program to input the integer array of size 3 × 3 and display the
transpose of it.
• Write a program to input the integer array of size 4 × 4 and find the
sum of each row of the array and display it.
• Write a program to input the integer array of size 3 × 3 and check it is
special matrix or not.
(A matrix is said to be special matrix if the sum of each row is equal
to sum of each column and equal to sum of left diagonal and equal to
the sum of right diagonal also.
Example:
4 9 2
3 5 7
8 1 6
Sum of each row = 15
Sum of each column = 15
Sum of left diagonal = 15
Sum of right diagonal = 15