Arrays
CPS 204 – Data Structures
Content
1. Introduction
2. One Dimensional Array
Array Declaration
Array Initialization
Processing Arrays
Using Loop
3. Multidimensional arrays
Array Declaration
Array Initialization
Processing Arrays
Using Loop
Content Cont……………….
1. Method in array
2. Class in array
3. Techniques
Sorting
Selection sort
Insertion sort
Searching
Binary search (sorted array)
Introduction
Array: An ordered collection of values with two
distinguishing characters:
Ordered and fixed length
Homogeneous. Every value in the array must be of the
same type
The individual values in an array are called elements.
The number of elements is called the length of the
array
Each element is identified by its position number in
the array, which is called index. In Java, the index
numbers begin with 0.
Introduction Cont……….
Parts of an Array
Sales
array
17.00 Sales[0]
name
17.95 Sales[1]
12.98 Sales[2]
112.49 Sales[3]
array
array 0.00 Sales[4]
indices
elements 3.00 Sales[5]
(subscripts)
65.29 Sales[6]
0.00 Sales[7]
72.98 Sales[8]
0.00 Sales[9]
One-Dimensional Arrays
Array declaration
An array is characterized by
Element type
Length
type[ ] Identifier = new type[length];
Example: An array of 5 instances of the student class
int[] topStudents = new Student[5];
1D Array Initialization
Arrayscan be initialized during their
declaration
One by one initialization
int arr[] = new int[4]
arr[0]=2;
arr[1]=3;
arr[2]=4;
arr[3]=6;
collection initialization
int arr[] = {98, 87, 92, 79, 85};
1D Processing Arrays
• Requires
– array name,
– subscript labeling individual element
• Syntax: name[subscript]
• Example
Sales[5] refers to the sales totals for employee
1D Processing Arrays
cont….
Public class example{
public static void main(String[] arg){
int [] arr = new int[4]
arr[0]=2;
arr[1]=3;
arr[2]=4;
arr[3]=6;
System.out.println( arr[0]);
}
}
1D Processing Arrays
cont….
Public class example{
public static void main(String[] arg){
int [] arr = {98, 87, 92, 79, 85};
System.out.println( arr[0]);
}
}
Accessing Array Elements
in a Loop
A for loop is commonly used to access array elements
For example:
int i;//loop counter/array index
int[] a = new int[4];
a[0]=2;
a[1]=3;
a[2]=4;
a[3]=6;
for(i = 0; i < a.length; i++){
System.out.println( a[i]);
}
Accessing Array Elements
in a Loop
A for loop is commonly used to access array elements
For example:
int i;//loop counter/array index
int a[] = {98, 87, 92, 79, 85};
for(i = 0; i < a.length; i++){
System.out.println( a[i]);
}
Initializing and Accessing
Array Elements in a Loop
A for loop is commonly used to access array elements
For example:
int i;//loop counter/array index
int[] a = new int[4];
for(i = 0; i < a.length; i++){
A[i]=i+1;
}
for(k = 0; k < a.length; k++){
System.out.println( a[k]);
}
Two-dimensional Arrays
A two-dimensional array consists of both rows and columns of elements.
Multidimensional arrays can be described as "arrays of arrays". For example,
a bidimensional array can be imagined as a two-dimensional table made of
elements, all of them of a same uniform data type.
Two-dimensional Arrays
jimmy represents a bidimensional array of 3 per 5
elements of type int.
Two-dimensional Arrays
and, for example, the way to reference the second
element vertically and fourth horizontally in an
expression would be: jimmy[1][3]
2D Declaration Arrays
(cont.)
The number-of-rows and number-of-
columns must be specified before declaring
the array.
General array declaration statement:
Data-type[][] array-name =new [number-of-rows]
[number-of-columns];
2D initializing Arrays
(cont.)
Arrays can be initialized during their declaration
int [][]a= { {98, 87, 92}, {79, 85, 19}, {32, 18, 2} };
The compiler fills the array row by row (elements are stored in
the memory in the same order).
Accessing 2D Array
Elements
A for loop is commonly used to access array elements
For example:
int [][]hk = { {98, 87, 92}, {79, 85, 19}, {32, 18, 2} };
int i,k;//loop counter/array index
for(i = 0; i <3; i++){
for(k = 0; k <3; k++){
System.out.println( hk[i][k]);
}
}
Use input from keyboard
The word scanner enable us to input from
keyboard.
Used import java.util.Scanner; method
int inter;
scanner tara =new scanner(system.in);
System.out.println( “inter number ”);
inter= tara.nextInet();
Use input from keyboard
import java.util.Scanner;
Public class example{
public static void main(String[] arg){
int[]numbersayi= new int[10];
int inter;
Scanner tara =new Scanner(System.in);
for(int i = 0; i < numbersayi.length; i++){
System.out.println( "inter number");
inter= tara.nextInt();
numbersayi[i]= inter;
}
for(int k = 0; k < numbersayi.length; k++){
System.out.println(numbersayi[k]);
}
}
}
Method in array
Public class example{
public static void main(String[] arg){
//Statement…….
//Calling of the method
}
public static void methodarray(int x){
// Statement…….
}
Method in array
Public class example{
public static void main(String[] arg){
int [] arr = new int[4]
arr[0]=2;
arr[1]=3;
arr[2]=4;
arr[3]=6;
methodarray(arr[1]);
}
public static void methodarray(int x){
System.out.println( x);
}
}
Method in array
Public class example{
public static void main(String[] arg){
int [] arr = new int[4]
arr[0]=2;
arr[1]=3;
arr[2]=4;
arr[3]=6;
methodarray(arr);
}
public static void methodarray(int []array){
for(int i = 0; i <array.length; i++){
System.out.println( array[i]);
}
}
}
Method in array
Access even and odd number
Count
Average
Sum
Square element
Access even
for(int i = 0; i <arr.length; i++){
if(arr[i]%2==0){
System.out.println( arr[i]);
}
odd number
for(int i = 0; i <arr.length; i++){
if(arr[i]%2!=0){
System.out.println( arr[i]);
}
Sum
int total=0;
for(int i = 0; i <arr.length; i++){
total=arr[i]+total;
}
System.out.println( total);
}
Techniques
Search Operation
Binary search (sorted array)
Insertion sort
Deletion
Linear Search
Linear Search is defined as a sequential
search algorithm that starts at one end and goes
through each element of a list until the desired
element is found, otherwise the search continues till
the end of the data set.
boolean flag=false;
int[]arr1 = {1,2,3,5,6,7};
int searchkey =10;
for(int i = 0; i < arr1.length; i++){
if(arr1[i]==searchkey){
System.out.println( arr1[i]);
flag=true;
break;
}if(flag==false){
System.out.println("nofound" );
break;
}
Linear Search duplicate
int[]arr1 = {1,2,3,1,6,1};
for(int i = 0; i < arr1.length; i++){
for(int k = i+1; k < arr1.length; k++){
if(arr1[i]==arr1[k]){
System.out.println( arr1[k]);
}
}
}
Linear Search Cont…
Advantages of Linear Search:
• Linear search can be used irrespective of whether the array is sorted or
not. It can be used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
Drawbacks of Linear Search:
• Linear search has a time complexity of O(N), which in turn makes it slow
for large datasets.
• Not suitable for large arrays
Binary Search
Binary Search is defined as a searching algorithm used in
a sorted array by repeatedly dividing the search interval in
half. The idea of binary search is to use the information
that the array is sorted and reduce the time complexity to
O(log N).
int seach=9;
int l=0;
boolean flag=false;
int[]arr1 = {1,2,3,4,6,7,8,10};
int h=arr1.length;
while(l<=h){
int m=(l+h)/2;
if(arr1[m]==seach){
// System.out.println( arr1[m]);
System.out.println( "element found:"+"" + arr1[m]);
flag=true;
break;
}if(arr1[m]<seach){
l=m+1;
}if(arr1[m]>seach){
h=m-1;
}
}if(flag==false){
System.out.println( "element not found:" + seach);
}
import java.util.Arrays;
int[]arr1 = {1,2,3,4,6,7,8,10};
System.out.println( Arrays.binarySearch(arr1,10));
It will return index if found the value, if not found
negative index
Binary search Cont…
• Binary search
– Can only be used on sorted arrays
– Compares middle element with key
• If equal, match found
• If key < middle, repeat search through the first half
of the array
• If key > middle, repeat search through the last half of
the array
– Very fast; at most n steps, where 2^n > # of
elements
• 30 element array takes at most 5 steps
– 2 > 30
Insert Operation
Insert at the end:
In an unsorted array, the insert operation is faster as compared to a sorted array because we
don’t have to care about the position at which the element is to be placed.
int[]arr2 = {1,2,3,5,6,7,0};
int ind=5;
int insrt =10;
for(int i = arr2.length-1; i >ind; i--){
arr2[i]=arr2[i-1];
arr2[ind]=insrt;
}for(int k = 0; k < arr2.length; k++){
System.out.println( arr2[k]);
}
Delete Operation:
In the delete operation, the element to be deleted is searched
using the linear search, and then the delete operation is
performed followed by shifting the elements.
import java.util.Arrays;
int[]arr1 = {1,2,3,5,6,7};
int[] arr2 =new int[arr1.length-1];
int ind=1;
for(int k = 0,i=0; k < arr1.length; k++){
if(k !=ind){
arr2[i]=arr1[k];
i++;
}
}
System.out.println( Arrays.toString(arr2));