Array in Java – With some Interesting Examples


Java / Sunday, July 21st, 2019

Introduction to Array in Java

Array in Java represents a number of variables which occupy contiguous spaces in the memory. Each element in the array is distinguished by its index. All elements in an array must be of the same data type. For example, you cannot have one element of the int data type and another belonging to the boolean data type in the same array.

An array is a collection of elements of the same type that are referenced by a common name. Each element of an array can be referred to by an array name and a subscript or index. To create and use an array in Java, you need to first declare the array and then initialize it.

Syntax:

data type variablename[];

Example:

int numbers[];

The above statement will declare a variable that can hold an array of the int type variables. After declaring the variable for the array, the array needs to be allocated in memory. This can be done using the new operator in the following way:

Numbers = new int[10];

This statement assigns ten contiguous memory locations of the type int to the variable numbers. The array can store ten elements. Iterations can be used to access all the elements of the array, one by one.

 

 Program 01

Write a program to input integer elements into an array of size 10 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.

Output:

Enter 10 integers:
12 45 -54 89 78 75 85 -32 66 -100
Largest element is: 89
Smallest element is: -100
Sum of elements is: 264

 Program 02

Write a program to input 10 words in an array. Arrange these words in descending order of alphabets, using selection sort technique. Print the sorted array.

Output:

Enter any 10 words:
Ram
Shyam
Jadhu
Modhu
Krishna
John
Ryan
Sachin
Sourav
Ajhar
The sorted words in descending order after selection sort technique:
Sourav
Shyam
Sachin
Ryan
Ram
Modhu
Krishna
John
Jadhu
Ajhar

 Program 03

Write a program to input 10 names in an array. Arrange these names in descending order of alphabets, using the bubble sort technique.

Output:
Enter any 10 names:
Ram
Shyam
jadhu
modhu
Krishna
Ryan
Ajhar
Rinku
Nilu
Tinku
The sorted words in descending order after bubble sort technique:
Tinku
Shyam
Ryan
Rinku
Ram
Nilu
modhu
Krishna
jadhu
Ajhar

 Program 04

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”.

Output:

Enter the country to be Searched: India
India – Tajmahal

Again after running

Enter the country to be Searched: England
Sorry Not Found!!!

 Program 05

Write a program to accept the year of graduation from school as an integer value from the user. Using the Binary Search technique on the sorted array of integers given below:
Output the message “Record exists” If the value input is located in the array. If not, output the message “Record does not exists”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}

Output:

Enter year: 2007
Record exists

Again after running

Enter year: 1992
Record does not exists

 

<< Previous  Next>>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *