CH 6 Arrays: Array Is A Collection of Variables of The Same Type That Are Referenced by A
CH 6 Arrays: Array Is A Collection of Variables of The Same Type That Are Referenced by A
Advantages of Arrays
a)Easy to specify
b)Free from run-time overheads
c)Random access of elements
d)Fast sequential access
e)Simple code
Disadvantages of Arrays
a)Need to know the size
b)careful design required
c)Not suitable for situations demanding varying memory-sizes
Array Declaration
dataType arrayName[arraySize];
For example,
float mark[5];
1
ICSE Class 10 Computer Applications
Array initialization
OR
2
ICSE Class 10 Computer Applications
//2. To accept and display the array elements
import java.util.*;
3
ICSE Class 10 Computer Applications
//3.To find sum,average,min and max of 5 numbers
import java.util.*;
for(i=0;i<5;i++)
{
A[i]=kb.nextInt(); //to accept array elements
sum=sum+A[i]; //to find the sum of array elements
}
4
ICSE Class 10 Computer Applications
//4. Linear Search
import java.util.*;
System.out.println("Enter 5 elements");
for(i=0;i<5;i++)
{
A[i]=kb.nextInt(); //to accept array elements
for(i=0;i<5;i++)
{
if(A[i]==sh)
k=1;
}
if(k==1)
System.out.println("The required no " + sh + " is present" );
else
System.out.println("The required no " + sh + " is not present" );
}
}
5
ICSE Class 10 Computer Applications
//5. Bubble Sort - Numbers
public class BubbleSortEg
{
public void bubbleSort(int A[])
{
int i,j,t;
6
ICSE Class 10 Computer Applications
//6. Bubble Sort - Strings
public class BubbleSortStringEg
{
public void bubbleSort(String A[])
{
int i,j;
String t;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(A[j].compareTo(A[j+1])>0)
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
}
7
ICSE Class 10 Computer Applications