Chapter 15
Arrays
Definition:
1. Array is defined as collection of similar type of data values.
2. An array is container object that contains/stores a fixed number of values of
similar type.
3. An array is a memory structure created that contains a number of values with
same variable name but different subscript (index) in order to deal with a number of
data values
Need of array
When a variable is declared, user is allowed to just assign a single value to a
variable. As soon as the user assigns a new value to a variable the previous value is
automatically replaced by the new value. This shows that a variable is capable of
storing a single value at once. If we want to store multiple values in a variable at
once, we use a logical structure known as an array.
Advantages of array
1. Allows storing multiple elements (values) of same type using a single name
(variable).
2. Easier access to any element using the index.
3. Easy to manipulate and store large data.
Disadvantages of array
1. Fixed size. Cannot be increased or decreased once declared.
2. Can store a single type of values only.
Single Dimensional Array
Characteristics
1. Fixed Length
2. Can store both primitive and non primitive values
3. Array variable is itself an object or reference variable
4. Array’s index starts with 0 and ends with N-1. Here, N stands for the number
of elements.
5. Elements are entered and accessed using same function.
function – arrayname[index].
6. Length of an array can be returned using the length (a final variable that stores
the number of element that can be entered in the array).
int var = arrayname.length;
Creating and initializing the single dimensional array
1. Using new keyword (dynamic)
Syntax –
<datatype> <array name []> = <new> <datatype[size]>;
for(int i=0;i< arrayname.length; i++)
, {
arrayname[i] = reading (inputting) methods ;
}
Example –
int a[] = new int[10];
for(int i=0;i< a.length; i++)
{
a [i] = Scanner object.nextInt() ; //or
Integer.parseInt(BufferedReaderobject.readLine());
}
2. Direct creation (static)
Syntax –
<datatype> <array name []> = {element1, element2, ..... element };
Example –
int a[] = {10, 20, 30, 40, 50};
Operations performed in array:-
1. Searching
2. Sorting
3. Mapping
4. Deletion
5. Insertion
6. Merging
Searching
Searching is the operation or process performed in an array to search for an element
whether it is available or not. Searching is done by two methods. –
1. Linear Search
2. Binary Search
1. Linear Search – Linear Search uses the method of traversing (traversal is a
method in which all the elements of the array are accessed). In this the
element is compared and searched from the first index number (i.e with the
first element) to the last index (till the last element). The process of
comparison and searching is continued till the element is searched (if present).
import java.util.*;
class Linear_Search
{
public static void main()
{
Scanner ob = new Scanner(System.in);
Arrays
Definition:
1. Array is defined as collection of similar type of data values.
2. An array is container object that contains/stores a fixed number of values of
similar type.
3. An array is a memory structure created that contains a number of values with
same variable name but different subscript (index) in order to deal with a number of
data values
Need of array
When a variable is declared, user is allowed to just assign a single value to a
variable. As soon as the user assigns a new value to a variable the previous value is
automatically replaced by the new value. This shows that a variable is capable of
storing a single value at once. If we want to store multiple values in a variable at
once, we use a logical structure known as an array.
Advantages of array
1. Allows storing multiple elements (values) of same type using a single name
(variable).
2. Easier access to any element using the index.
3. Easy to manipulate and store large data.
Disadvantages of array
1. Fixed size. Cannot be increased or decreased once declared.
2. Can store a single type of values only.
Single Dimensional Array
Characteristics
1. Fixed Length
2. Can store both primitive and non primitive values
3. Array variable is itself an object or reference variable
4. Array’s index starts with 0 and ends with N-1. Here, N stands for the number
of elements.
5. Elements are entered and accessed using same function.
function – arrayname[index].
6. Length of an array can be returned using the length (a final variable that stores
the number of element that can be entered in the array).
int var = arrayname.length;
Creating and initializing the single dimensional array
1. Using new keyword (dynamic)
Syntax –
<datatype> <array name []> = <new> <datatype[size]>;
for(int i=0;i< arrayname.length; i++)
, {
arrayname[i] = reading (inputting) methods ;
}
Example –
int a[] = new int[10];
for(int i=0;i< a.length; i++)
{
a [i] = Scanner object.nextInt() ; //or
Integer.parseInt(BufferedReaderobject.readLine());
}
2. Direct creation (static)
Syntax –
<datatype> <array name []> = {element1, element2, ..... element };
Example –
int a[] = {10, 20, 30, 40, 50};
Operations performed in array:-
1. Searching
2. Sorting
3. Mapping
4. Deletion
5. Insertion
6. Merging
Searching
Searching is the operation or process performed in an array to search for an element
whether it is available or not. Searching is done by two methods. –
1. Linear Search
2. Binary Search
1. Linear Search – Linear Search uses the method of traversing (traversal is a
method in which all the elements of the array are accessed). In this the
element is compared and searched from the first index number (i.e with the
first element) to the last index (till the last element). The process of
comparison and searching is continued till the element is searched (if present).
import java.util.*;
class Linear_Search
{
public static void main()
{
Scanner ob = new Scanner(System.in);