0% found this document useful (0 votes)
2 views11 pages

Array

The document provides an overview of arrays in Java, explaining their definition, usage, declaration, creation, initialization, and types. It also covers searching techniques such as Linear and Binary Search, including example programs and explanations for each method. Additionally, it highlights common errors and offers practice programs for better understanding of arrays.

Uploaded by

Mridul Ghosh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Array

The document provides an overview of arrays in Java, explaining their definition, usage, declaration, creation, initialization, and types. It also covers searching techniques such as Linear and Binary Search, including example programs and explanations for each method. Additionally, it highlights common errors and offers practice programs for better understanding of arrays.

Uploaded by

Mridul Ghosh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array

1. What is an Array?

• An array is a collection of elements of the same data type stored in contiguous memory locations.

• It is used to store multiple values in a single variable.

Example:

int marks[] = {85, 90, 78, 92};

2. Why Use Arrays?

• To store many values together.

• Easy to access data using index numbers.

• Reduces the need for multiple variables.

3. Declaration of Array

Syntax:

datatype arrayName[];

or

datatype[] arrayName;

Example:

int num[];
String names[];

4. Creation of Array

Syntax:

arrayName = new datatype[size];

Example:

num = new int[5];

This creates an array of 5 integers.

5. Declaration + Creation Together

int num[] = new int[5];


6. Initialization of Array

int num[] = {10, 20, 30, 40, 50};

7. Index Number

• Array index starts from 0.

• First element → index 0

• Last element → size - 1

Example:

num[0] = 10;
num[1] = 20;

8. Accessing Elements

[Link](num[2]);

Output:

30

9. Changing Value

num[1] = 99;

10. Length of Array

[Link]

Used to find total number of elements.

11. Traversing an Array using Loop

for(int i=0; i<[Link]; i++)


{
[Link](num[i]);
}

12. Types of Arrays in Java

(a) One-Dimensional Array

Stores values in a single row.


int a[] = {1,2,3};

(b) Two-Dimensional Array

Stores values in rows and columns.

int arr[][] = new int[2][3];

13. Default Values in Array

When array is created:

• int → 0

• double → 0.0

• char → blank (\u0000)

• boolean → false

• String → null

14. Common Errors

ArrayIndexOutOfBoundsException

Occurs when index exceeds size.

int a[] = new int[3];


a[5] = 10; // Error

15. Important Programs for Practice

1. Find sum of array elements

2. Find largest element

3. Find smallest element

4. Search an element

5. Reverse array

6. Count even and odd numbers

Sample Program

class Demo
{
public static void main(String args[])
{
int a[] = {10,20,30,40};

for(int i=0; i<[Link]; i++)


{
[Link](a[i]);
}
}

Linear Searching Program in Array in Java

What is Linear Search?

Linear Search is a searching technique in which each element of the array is checked one by one until the
required element is found.

It is the simplest searching method.

Java Program

class LinearSearch
{
public static void main(String args[])
{
int arr[] = {12, 25, 8, 45, 30};
int search = 45;
int found = 0;

for(int i = 0; i < [Link]; i++)


{
if(arr[i] == search)
{
[Link]("Element found at position: " + (i + 1));
found = 1;
break;
}
}

if(found == 0)
{
[Link]("Element not found");
}
}
}

Explanation of the Program

1. Declaring the Array

int arr[] = {12, 25, 8, 45, 30};

This creates an integer array containing 5 elements.

Index Value

0 12

1 25

2 8

3 45

4 30

2. Element to be Searched

int search = 45;

We want to find the number 45 in the array.

3. Variable for Checking

int found = 0;

• found = 0 means element not found yet.

• If found, it becomes 1.

4. Loop for Searching

for(int i = 0; i < [Link]; i++)

This loop checks each element one by one.

[Link] gives total number of elements = 5

So loop runs from index 0 to 4.

5. Condition Checking
if(arr[i] == search)

If current array element matches the searched number.

At index 3, value is 45, so condition becomes true.

6. Display Result

[Link]("Element found at position: " + (i + 1));

Since array index starts from 0, position is shown as i + 1

Index 3 = Position 4

7. Break Statement

break;

Stops the loop after finding the element.

8. If Element Not Found

if(found == 0)
{
[Link]("Element not found");
}

If no match occurs, this message is displayed.

Output

Element found at position: 4

How Linear Search Works

Searching 45

• Check 12

• Check 25

• Check 8

• Check 45 Found
Binary Search Program in Array in Java

What is Binary Search?

Binary Search is a searching technique used to find an element in a sorted array.


It repeatedly divides the array into two halves until the element is found.

• Faster than Linear Search


• Works only on sorted arrays

Condition for Binary Search

The array must be in ascending or descending order.

Example (Ascending Order):

int arr[] = {10, 20, 30, 40, 50, 60};

Java Program

class BinarySearch
{
public static void main(String args[])
{
int arr[] = {10, 20, 30, 40, 50, 60};
int search = 40;

int low = 0;
int high = [Link] - 1;
int mid;
int found = 0;

while(low <= high)


{
mid = (low + high) / 2;

if(arr[mid] == search)
{
[Link]("Element found at position: " + (mid + 1));
found = 1;
break;
}
else if(search < arr[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}

if(found == 0)
{
[Link]("Element not found");
}
}
}

Explanation of the Program

1. Declaring Sorted Array

int arr[] = {10, 20, 30, 40, 50, 60};

Index Value

0 10

1 20

2 30

3 40

4 50

5 60

2. Search Element

int search = 40;

We want to search number 40.

3. Initial Variables

int low = 0;
int high = [Link] - 1;

• low = 0 → first index

• high = 5 → last index


4. While Loop

while(low <= high)

Loop runs until search range is valid.

5. Finding Middle Index

mid = (low + high) / 2;

First time:
0+5
𝑚𝑖𝑑 = =2
2
So middle element = arr[2] = 30

6. Compare with Search Element

Case 1: If Found

if(arr[mid] == search)

If equal, element found.

Case 2: Search Smaller

else if(search < arr[mid])

Search left half.

Case 3: Search Greater

else
{
low = mid + 1;
}

Search right half.

Dry Run of Program

Searching 40

Step 1

• low = 0

• high = 5
0+5
𝑚𝑖𝑑 = =2
2
Value = 30

40 > 30 → Search right side

Now:

• low = 3

• high = 5

Step 2
3+5
𝑚𝑖𝑑 = =4
2
Value = 50

40 < 50 → Search left side

Now:

• low = 3

• high = 3

Step 3
3+3
𝑚𝑖𝑑 = =3
2
Value = 40 Found

Output

Element found at position: 4

(Index 3 = Position 4)

Important Notes

• Array must be sorted


• Faster than Linear Search
• Uses divide and search method
• Suitable for large data
Difference Between Linear Search and Binary Search

Linear Search Binary Search

Checks one by one Checks middle element

Slow Fast

Works on unsorted array Needs sorted array

You might also like