You are on page 1of 31

ARRAYS

PRPARED BY Pragyan Nanda


Array

 It is an ordered collection or numbered list of values that are stored in


contagious memory location.
 The stored value in an array can be of primitive or non primitive type
 The data type of array elements is known as the base type of the array.
 Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created. After creation, its length is fixed.

An array of 10 elements.

Each item in an array is called an element, and each


element is accessed by its numerical index. As shown in
the preceding illustration, numbering begins with 0. The
9th element, for example, would therefore be accessed at
index 8.
Subscript /index
 A subscript is an integer value between [ ]
 That represents the index/position or location of the element you want to
get to.
 The special symbols [ ] represent the mathematical subscript notation.
 Java uses x[0], x[1], and x[n-1].
 The subscript is used for accessing individual elements in an array.
 It is always started from 0.
 In the java arrays the element at the position 1 has index/subscript 0,
element at position 2 has index 1 and so on. Thus position of an element
is index+1.
Declare an array
 Like other variables in java arrays must be declared before they can
be used
 The syntax is
 Type arrayname[]= new type[size];
 Or type[] arrayname= new type [size]; // this is the perfect way to
declare because the brackets identify the array type and should appear
with the type designation.
 Here type –is the base type or type of each element present in array
 arrayname- name of the array
 Size- the no of elements contained in the array
 Example : int roll[]= new type[10];
 It instructs the computer to define a roll array that can have 10
elements
Creating, Initializing, and Accessing an
Array
One way to create an array is with the new operator.
When an array object is created with the new operator , its elements are
automatically initialized to zero, which is the default intial values for all
numeric types
It allocates an array with enough memory for 10 integer elements and assigns
the array to the roll variable.
// create an array of integers
roll = new int[10];
If this statement is missing, then the compiler prints an error like the following,
and compilation fails:
ArrayDemo.java:4: Variable roll may not have been initialized.
The next few lines assign values to each element of the
array:
roll[0] = 1;
// initialize first element
roll[1] = 2;
// initialize second element
roll[2] = 3;
// and so forth
Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + roll[0]);
System.out.println("Element 2 at index 1: " + roll[1]);
System.out.println("Element 3 at index 2: " + roll[2]);
Alternatively, you can use the shortcut syntax to create and initialize an
array:

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

Here the length of the array is determined by the number of values


provided between braces and separated by commas.
int [] roll=new int[10];
ARRAYNAME

roll[10] ELEMENT
NAME
1 Roll[0]
2 Roll[1]
3 Roll[2]
4 Roll[3]
5 Roll[4]
6 Roll[5]
Roll[6] SUBSCRIPT/IND
7
EX
8 Roll[7]
9 Roll[8]
10 Roll[9]

SINGLE DIMENSIONAL ARRAY


Storing values in a single dimensional array
Direct Assignment

}
}
Using an array initialiser

}
Using Method Argument in BLUEJ
Using scanner class
Out of bound indices

The subscripts other than 0 to n-1 (both limits inclusive)


for an array having n elements , are called out-of –
bounds subscripts.
Two types of array

 1.Single dimensional array or 1-D array.:- When the elements of an array are
specified using single index (one column and multiple rows)
 In a single dimensional array , the elements are stored in contiguous memory
locations in order of their index.
 2.Multidimensional array :- Multiple rows and columns
1 [0][0] 2 [0][1] 3 [0][2]
4 [1][0] 5 [1][1] 6 [1][2]

7 [2][0] 8 [2][1] 9 [2][2]


 Int roll[][]= new int[3][3];
 Total element in array= no of rows*no of columns
Finding a length of an Array

 The length or size of an array is the number of elements contained in it.


 You can use the length property to determine the size of an array.
 The length of an array is very important as it is use in the iteration , that for
doing the maximum number of times the loop has to be iterated.
 Syntax :-
 Array name.length ( here the length is property that returns the number of
elements in the array.
 Example suppose roll is the array name then
 int len= roll.length
Program

public class arrlength


{
Public static void main(String args[]) Output
The length of an array is:- 5
{
Int roll[]={1,2,3,4,5};
Int len=roll.length;
System.out.println(“ The length of an array is:-”+len);
}
}
Searching an element from an array
(Linear search)
 Linear search refers to the searching technique in which each element of an
array is compared with the search no, one by one , until the search no , is
found or all the elements have been compared.
 Or
 The search technique that traverses the array sequentially to locate a given
value is known as linear or sequential search.
 A linear search can be used with both sorted and unsorted (arranged order/ or
not in a order) array.
 In searching there are two possibility outcome either the search no present in
array or not present in an array.
list[5] Searchno-52

Position of elements

1 2 3 4 5
99 25 45 52 60

0 1 1 3 4
list[0] list[1] list[2] list[3] list[4]

Subscript / index
Output:-
The element
found in (3+1)
4th location
import java.io.*;
import java.util.Scanner;
public class linear
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int searchno;
int found=0;
int list[]={99,25,45,52,60};//creating and initializing array with value
System.out.println("Enter the no to be search:");
searchno=sc.nextInt();
int len1=list.length;
for(int i=0;i<len1;i++) // searching start from ist location till end or till search no found
{
if(list[i]==searchno) // searching
{
found=i;
break;
}
}
if(found !=0) // checking
{
System.out.println(" The element found in :"+(found+1)+ "location");
}
else
{
System.out.println(" The element not found ");
}
}// main end
}// class end
 INPUT :- Enter the no to be search:52

 OUTPUT:- The element found in : 4TH location

 INPUT :- Enter the no to be search:99

 OUTPUT:- The element not found


Explanation

 1. Initialize the found variable (status of searching) to =0 at beginning


 2. accept the number to be searched(searchno).
 3. Find the length of array(lis.length)
 4. do the iteration, the starting at 0 and ending boundary as less then the
array length(i=0;i<len;i++)
 5. compare the searchno with each and every elementst(searchno==list[i])
 6. as soon as the search found change the status of found as 1and terminate
the loop otherwise search till end
 7. check if found is not equal to 0 then searching successful the display the
position by index + 1 ( for getting correct position of the variables).
 Else display the message element not found.
Sorting an array(Bubble Sort)

 Sorting is the process of arranging the array elements in ascending or


descending order.
 The idea of bubble sort is to move the largest element to the highest index
position in the array. To attain this two adjacent elements are compared
repeatedly and exchanged if they are not in correct order.
 Let us understand this technique

0 1 2 3 4 5 6 7
65 47 70 9 37 72 45 17
 Unsorted array with indices
1.Ist the element in 0 and 1 index position are compared. There positions are
swapped if the element at 0th index is greater than the element at the ist index.
Since 65> 47 a swap occurs.

1 2 3 4 5 6 7
0
65 47 70 9 37 72 45 17

2.Now , the elements at the ist and 2nd index are compared. Since 65 < 70
no swap occurs.
0 1 2 3 4 5 6 7
47 65 70 9 37 72 45 17
3.he elements at the 2nd and 3rd index are compared . Since 70>9 , a swap occurs
. SWAP
0 1 2 3 4 5 6 7
47 65 70 9 37 72 45 17

4. The elements at the 3rd and the 4th index are compared.

0 2 3 4 5 6 7
47 65 9 70 37 72 45 17
 70>37, yes so swap occurs
5. The elements at the 4th and the 5th index are compared. Since
70<72 no swap occurs
0 1 2 3 4 5 6 7

47 65 9 37 70 72 45 17

6.The elements at the 5th and the 6th index are compared .
Since 72>45 , a swap occurs

0 1 2 3 4 5 6 7
47 65 9 37 70 72 45 17
7. The elements at the 6th and 7th index are compared. Since 72> 17 , a swap occurs
0 1 2 3 4 5 6 7

47 65 9 37 70 45 72 17

8. This results in a list with the highest element in the last position
0 1 2 3 4 5 6 7

47 65 9 37 70 45 17 72

The element at the 7th index in its


proper position
Explanation
 1.Store unsorted element in array named list.
 2.Stores the length of list in the len variable.(list.length)
 3. The outer loop iterates through the entire array. (i=0;i<len-1;i++)
 (that means it started from ist location and loop continue till last location , it is started from 0
so last location is len-1)
 4. The inner loop iterates through the remaining unsorted array only. This has been achieved by
subtracting the value of counter variable i in the inner loop test condition(len-i-1)
 5.Compares the two adjacent element , i.e , element at jth position and that at (j+1)nth
position.
 6. Stores the value of the current element in a temporary variable.
 8. Stores the value of the next element of the array in the current element.
 9. Stores the value of the temporary variable in the next element.

You might also like