Professional Documents
Culture Documents
HOME
ABOUT
ARRAYS
ICSE THEOREY
MOBILE APP
- T E JA S WA ROOP A ND
AR RAY S
BUNCY -
bluejicse
Tech Raj
227 likes
Like Page
Be the first of your
friends to like this
Instead of creating different variables in order to assign different values, by using arrays we can
declare more than one element or value under the same declaration, we can also allot the space
for each Array in Java.Therefore, we need not create a sepearate variable for each separate
variable.
Arrays also makes it easier to perform calculations in the loop
Example : Consider :
for(int i=1;i<=10;i++)
int m = Integer.parseInt(in.readLine());
In the above Java statements , we can input the value of the variable m for 10 times using the for
loop as shown , but as soon as the next value of the variable m is entered by the user the
previous value of the m gets deleted , therefore , If we want to calculate the sum of the 10 values
entered by the user , it is not very easy , therefore , to perform this activity , we can use Arrays to
make it easier and simple to avoid confusion for better programming.
Web page converted to PDF with the PDFmyURL PDF creation API!
Tech
Raj
April 21 at
9:46am
Web page converted to PDF with the PDFmyURL PDF creation API!
m[0] = 12
m[1] = 58
m[2] = 69
m[3] = 87
m[4] = 45
m[5] = 78
m[6] = 25
m[7] = 36
Types of Arrays :
1.Single Dimensional array
2.Double Dimensional array
{
System.out.print(m[i][j]+ );
}
System.out.println();
}
}
}
Output of the above program :
We can input the elements of the array through bluej method , it can be done by declaring the
array in the method of the program , Example ,
static void teja(int m[])
In the above statement we have declared the array m , in the method teja of the program.
Example Program to input the elements of an array through Bluej Mehod :
class Arrays_Bluejmthd
{
static void test(int m[])
{
System.out.println(The Entered Array is : );
for(int i=0;i<m.length;i++)
{
System.out.print(m[i]+ );
}
}
}
Input :
Web page converted to PDF with the PDFmyURL PDF creation API!
for(int j=0;j<m.length;j++)
{
System.out.print(m[j]+ );
}
}
}
In the above program the highlighted part plays the real role in arranging the elements entered
by the user in the array.
Input,
1.Searching
Searching is a process in which the entered value is checked with the already present values to
find out whether the entered value is present in the array.
Two types of Searching Methods ,
(i).Linear Searching :
It is the method in which the the elements present in the array are checked from 0th position with
the value entered by the user , the checking starts from 0th position, if the element is present in
the array , it results as Search Successful else
it results as Search Unsuccessful
Example Program on Linear Searching ,
import java.util.*;
class LinearSearch
{
static void test(int m[])
{
int a=0;
Scanner in=new Scanner(System.in);
System.out.println(The Array obtained is : );
for(int k=0;k<m.length;k++)
{
System.out.println(m[k]);
}
System.out.print(Enter the number which is to be searched :);
Web page converted to PDF with the PDFmyURL PDF creation API!
int c=in.nextInt();
boolean isPresent = false;
for(int j=0;j<m.length;j++)
{
if(c==m[j])
a++;
}
if(a>=1)
System.out.println(Search Successful );
else
System.out.println(Search Unsuccessful );
}
}
Input ,
Entered array = {15,154,48,48,48}
was present !! :
import java.util.*;
class LinearSearch
{
static void test(int m[])
{
Scanner in = new Scanner(System.in);
System.out.println(Elements of the Array are : );
for(int i=0;i<m.length;i++)
System.out.print(m[i]+ );
System.out.println(\n\nEnter the number to be searched );
int c = in.nextInt();
int a = 0;
for(int j=0;j<m.length;j++)
{
if(c==m[j])
{
a++;
}
}
int[] s=new int[a];
int k=0;
for(int j=0;j<m.length;j++)
{
if(c==m[j])
{
a++;
s[k]=j;
k++;
Web page converted to PDF with the PDFmyURL PDF creation API!
}
}
if(a>=1)
{
System.out.print(\n\nSearch successful, +c+ is present at : );
for(int i=0;i<s.length;i++)
System.out.print(s[i]+ , );
System.out.print(Positions(s) );
}
else
System.out.println(\n\nSearch unsuccessful ! );
}
}
(ii)Binary Searching :
Binary search can only be applied to array whose elements are in either or descending order.
let us , take the array {4,7,8,10,14,21,22,36,62,77,81,91}
Here , there are 11 elements in the array , the process that happens in binary search is :
Step 1, The mid term of the array gets calculated , it will be calculated as (first element + last
element)/2 and based on the mid term , the array gets divided into two parts.
Step 2, The value entered by the user gets compared with the mid term let us say, the user entered
22 , now, the mid term i.e., 21<22 therefore, the search goes to the second half(greater half)
Web page converted to PDF with the PDFmyURL PDF creation API!
Step 3 , Then , the first half again gets divided into two halves by calculating the mid term of the
first half last 2= mid-1 ; mid2=first+last/2
Step 4 , if the value entered by the user matches with any of the element of the array , then the
search is said to be successful , else
the search is said to be unsuccessful.
For better understanding , refer the image below ,
}
if ( first > last )
System.out.println(search + is not present in the list.\n);
}
}
2.Sorting
Sorting is a process in which the elements of the array are either arranged in ascending or
descending order.
Two methods of sorting in JAVA ,
(i)Selection Sort :
It is the method in which the control starts from 0th position and it checks for the lowest value ,
and replaces it with the value present at the 0th position of the array , in such a way it replaces the
elements which are not in order , and thus results in ascending order.
Consider the array {5,1,12,-5,16,2,12,14} ,
Look at the image for better understanding
Web page converted to PDF with the PDFmyURL PDF creation API!
Finally ,
is the result
O
Document By Teja
Web page converted to PDF with the PDFmyURL PDF creation API!
SHARE THIS:
Like
Be the first to like this.
Web page converted to PDF with the PDFmyURL PDF creation API!
REPLY
L EAVE A R EP LY
Enter your comment here...
- TEJA -
- T E J A S W A R O O P A N D B U N C Y-
bluejicse
- C R E AT I V E B R A I N S 2 Web page converted to PDF with the PDFmyURL PDF creation API!
- TEJA -
- BUNCY - O U R P O S T S W I T H D AT E S - BUNCY S HA D D A I -
April 2016
M
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Feb
- C H AT N O W ! tlk .io
Web page converted to PDF with the PDFmyURL PDF creation API!
WordPress.com News 0
B L O G A T W O R D P R E S S . C O M . | T H E S E L A T H E M E.
Follow
Web page converted to PDF with the PDFmyURL PDF creation API!