100% found this document useful (6 votes)
8K views8 pages

Icse X Arrays

1. The document discusses arrays, which are sequences of variables of the same data type that provide effective memory management. Arrays can be single dimensional or double dimensional. 2. Single dimensional arrays contain one row for storing data and use a single set of brackets, while double dimensional arrays contain multiple rows and columns for storing data using double brackets. 3. The document provides examples of creating, initializing, inputting values to and outputting values from arrays in Java using for loops. It also discusses the length property and basic operations like searching that can be performed on arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (6 votes)
8K views8 pages

Icse X Arrays

1. The document discusses arrays, which are sequences of variables of the same data type that provide effective memory management. Arrays can be single dimensional or double dimensional. 2. Single dimensional arrays contain one row for storing data and use a single set of brackets, while double dimensional arrays contain multiple rows and columns for storing data using double brackets. 3. The document provides examples of creating, initializing, inputting values to and outputting values from arrays in Java using for loops. It also discusses the length property and basic operations like searching that can be performed on arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Sumit Sir Chapter-3 ICSE-X

Arrays (Single Dimensional & Double Dimensional)

An Array is a sequence of variable with same data type. Array provides an


effective way of memory management. Once you declare the size of an array it
comes fixed. The index of an array always starts with position 0. An array can be
used to represent a list of numbers, or a list of names.

Features of an Array
1. Arrays are objects.
2. They can even hold the reference variables of other objects.
3. Arrays are created during runtime.
4. They are dynamic, created on the heap.
5. The length of an array is fixed.

Why we need an Array?


Let us assume that while doing a programming you come to a situation where
you need to store 20 integer number in a variable. Then what will you do?
You have two options:
(1) Create 20 variables with int data types.
(2) You can go for creating an array of size 20.
If you go for the first option then your programme will get complex and so
lengthy that you yourself will get confuse.
So, it’s better to go with the second option. The array will use the same name
and data type. This variable will differ on their subscripts. We can access the
values store in an array by using a loop to change the subscripts.
Types of an Array
Arrays are of two types:
(1) Single Dimentional Array
(2) Double Dimentional Array
(1) Single Dimentional Array
The one dimensional array or single dimentional array is the simplest type of
array that contains only one row for storing data. It has single set of square
bracket ( “[]” ), we can also say that these arrays are single scripted array.
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)

Now let us discuss about the details of an array.


CREATING AN ARRAY:

int A[]=new int[10]; //integer array created of size 10

This syntax indicates that it can store 10 values in the memory with different
subscripts. Let’s see the structure of a single dimentional array for the above
syntax.
Array A[]
Superscripts A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Data items 12 22 33 56 90 100 23 67 29 87

In this figure the values inside the bracket [] are actually referred as subscripts
and they help in both input and output of the array elements. Here A is
subscripted variable.
We can create array of different data types as shown below:
int s[]=new int[10] //an integer array
float f[]=new float[10] //to store fractional number
char c[]=new char[10] //to store character
String s[]=new String[10] //to store string values
How to initialize an array
We can initialize an array by providing default values but while initializing it
keep in mind that you don’t need to supply the size of an array while initializing.
Let’s tale an example:

Initializing an integer array:


int i[]={1,2,3,4,5,6,7,8,9,10};

Initializing a character array:


char ch[]={‘a’,’b’,’c’,’d’,’e’};

Initializing a float array:


float f[]={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};

Initializing a String array:


String s[]={“Ankita”, “Guriya”, “Purna”};
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)
Why we have not declared the size of an array?
When we initialize the values in an array, we already know that how much
space we need in memory and how many values we need to supply to an array.
Basically, an array with empty [] represents infinite number of cells.
So, when we need to take input from the user for our declared array, at that
time we need to specify the size.
NOTE: There is a length keyword available in java to find the number of
subscripts in an array. It returns an integer value. It is to note that it is not a
function. Don’t get confuse between length() used in String function and length
in array.
Let us take an example:
class run class run
{ {
public static void main() public static void main()
{ {
int a[]=new int[10]; String s[]={“Ankita”, “Guriya”, “Porna”};
int n=a.length; int n=s.length;
System.out.println(n); System.out.println(n);
} }
} }
OUTPUT: 10 OUTPUT: 3

HOW TO TAKE INPUT AND DISPLAY THE CONTENT OF AN ARRAY:


The concept of loop is always used in an array. So every time when you need to
perform a task within an array you need the help of loops. Let’s see an
example:
Program: Write a program to input 10 numbers from user in an array and
display its contents.
import java.util.*;
class displaynumbers
{
public static void main()
{
int a[]=new int[10];
Scanner in=new Scanner(System.in);
System.out.println(“Enter 10 numbers in an array:”);
for(int i=0;i<10;i++)
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)

a[i]=in.nextInt(); INPUT LOOP


System.out.println();
System.out.println(“Displaying CONTENTS:”);
for(int j=0;j<10;j++)
System.out.print(a[j]+””); OUTPUT LOOP
}
}
BASIC OPERATIONS ON ARRAYS
Following are the basic operations which can be performed on arrays:
1. Searching
2. Sorting
3. Merging
4. Insertion
5. Deletion
1. Searching
This is one of the most basic operations which can be carried on arrays. It is the
process to determine whether a given item (i.e., a number/ a character/ a
word/ a name etc.) is present in the array or not. This can be done in two ways:
Searching

Linear Search/Sequential Search Binary Search

LINEAR SEARCH
Linear Search means sequential search. It is very simple and easy to implement. The
searching begins with the starting position of an array i.e., 0th index and continues to
search the element one after another till the element has been found. Once the
element has been found, it is then checked and compared with a given data item.
Linear Search is very slow process.
Let’s see how it works:
Let’s assume that we are having an array of 10 integer value and we need to check
whether a number 30 is present in it or not.
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)

Array A[]= 12 33 30 41 4 8 23 11 89 6
Item to search=30 b=30
Implementation : A Simple Linear Search Program
class linear
{
public static void main(String args[])
{
int A[]={12,33,30,41,4,8,23,11,89,6};
int b=30, f=0;
for(int i=0;i<10;i++)
{
if(A[i]==b)
{
f=1;
break;
}
}
if(f==1)
System.out.println("Number found successfully");
else
System.out.println("Number not found");
}
}
Now let us take another example of linear search by taking all the inputs from the
user and here we will also find the position number of the elements.
import java.util.*;
class positionnumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int A[]=new int[10];
int b, flag=0, pos=1;
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)
System.out.println("Enter 10 numbers in an array:");
for(int i=0;i<10;i++)
{
A[i]=in.nextInt();
}
System.out.println("Enter a number to be search:");
b=in.nextInt();
for(int j=0;j<10;j++)
{
if(A[j]==b)
{
flag=1;
pos=pos+j;
break;
}
}
if(flag==1)
System.out.println("Number found successfully at position:" +pos);
else
System.out.println("Number not found:");
}
}

Reason to use a variable flag?


Flag=0: We have made a flag variable and initialized it with zero by default because
while executing the program we are assuming that there is no search made and
hence no element has been found.
Flag=1: This condition has been made to make the interpreter understand that the
checking condition between the array and the data item (variable b) has been
satisfied and the item has been found in the array.
Why we have used break statement?
We know that break is a keyword and is used to terminate the loop or even program,
depends on where we placed it. Here we have used break inside the if condition to
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)
terminate the loop because as we find the item in the array, it is unnecessary to go
through the whole loop.
BINARY SEARCH
Binary Search is another technique to search for an element in the given array in
minimum possible time.
Let’s understand it by a simple scenario.
Let’s assume two cases:

Scenario 1: Suppose you have 10 elements in an array and you have to check
whether a particular element is present within the array or not. So, here we can
simply use the linear search because searching 10 elements is very much easier and it
will also not affect the performance of the system.

Scenario 2: Now let us assume that you have to search an element between 1 to 50
thousand and suppose the number you are searching is available at last index of the
array and now think of linear search that how much time it will take to move the
position from 0th index to last index.
Of course it will take a huge amount of time. So the developer has worked on these
and brought a concept of binary search.

Now let us discuss about the binary search.


Binary Search: Binary Search is a new technique which is used to search an element
in a minimum possible time. The binary search always works on the principal of
sorting data which means the data available in binary search must be in ascending or
descending order. In this technique, an array is divided into two halves, which means
the searching element may be present in first halves or second halves. It always
compares the searching element with the middle element of the sorted array.
NOTE: If your array is not sorted then first you have to sort it then perform binary
search.
Let us go through a demonstration on binary search working principal.
Suppose we need to find an element 20 from the given sorted array.

10 20 30 40 50 First=0, last=4 (Size of array is 5)


MID=(first+last)/2=(0+4)=2
0th 1st 2nd 3rd 4th
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)

Now compare the value 20 with mid i.e., 20>=30 as the condition is false the search
will take place in first half because the array has been sorted in ascending order now
the mid became the last index of the searching array.

First=0 (Remain same)


10 20 30 40 50
Last=2
0th 1st 2nd 3rd 4th Mid=(First+Last)/2=(0+2)=1

Now compare the value 20 with mid i.e., 20>=20 and hence condition is true and the
element has been found.

You might also like