1️⃣ What is an Array?
An array is a collection of elements of the same data type stored in continuous memory
👉
locations and referred to by a single name.
👉 Each element is accessed using an index number.
Indexing always starts from 0.
Example:
int marks[] = {45, 67, 89, 90};
Here:
• marks[0] = 45
• marks[1] = 67
2️⃣ Types of Arrays (ICSE)
1. Single Dimensional Array (1D)
2. Double Dimensional Array (2D)
3️⃣ Single Dimensional Array (1D Array)
🔹 Declaration
int a[];
OR
int[] a;
🔹 Initialization
int a[] = {10, 20, 30, 40};
OR
int a[] = new int[4];
🔹 Accepting Data (Using Loop)
Scanner sc = new Scanner([Link]);
int a[] = new int[5];
for(int i = 0; i < 5; i++)
{
a[i] = [Link]();
}
🔹 Accessing Elements
[Link](a[2]); // prints 3rd element
4️⃣ Uses of Arrays
✔ Store multiple values
✔ Easy data processing
✔ Used in sorting and searching
✔ Saves memory and time
5️⃣ Length Statement
Used to find size of array.
int n = [Link];
👉 Very important for exams.
6️⃣ Sorting Techniques (1D Array Only)
🔹 Selection Sort
Logic:
• Find smallest element
• Place it at correct position
• Repeat
Example Code:
for(int i = 0; i < [Link] - 1; i++)
{
int min = i;
for(int j = i + 1; j < [Link]; j++)
{
if(a[j] < a[min])
min = j;
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
🔹 Bubble Sort
Logic:
• Compare adjacent elements
• Swap if required
• Largest element moves to end
Example Code:
for(int i = 0; i < [Link] - 1; i++)
{
for(int j = 0; j < [Link] - i - 1; j++)
{
if(a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
7️⃣ Searching Techniques (1D Array Only)
🔹 Linear Search
Checks elements one by one
int flag = 0;
for(int i = 0; i < [Link]; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}
}
✔ Works on sorted and unsorted arrays
🔹 Binary Search
✔ Array must be sorted
int lb = 0, ub = [Link] - 1, mid;
while(lb <= ub)
{
mid = (lb + ub) / 2;
if(a[mid] == key)
break;
else if(key < a[mid])
ub = mid - 1;
else
lb = mid + 1;
}
8️⃣ Array as a Composite Type
An array is a composite data type because:
• It stores multiple values
• All elements are of same type
• Treated as a single unit
9️⃣ Double Dimensional Array (2D Array)
Used to store data in row and column format.
🔹 Declaration
int a[][];
🔹 Initialization
int a[][] = new int[3][3];
🔹 Accepting Data
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}
🔟 Display Matrix Format
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
[Link](a[i][j] + " ");
}
[Link]();
}
1️⃣ 1️⃣ Sum of Rows
1️⃣1️⃣
for(int i = 0; i < 3; i++)
{
int sum = 0;
for(int j = 0; j < 3; j++)
sum += a[i][j];
[Link]("Row sum = " + sum);
}
1️⃣ 2️⃣ Sum of Columns
1️⃣2️⃣
for(int j = 0; j < 3; j++)
{
int sum = 0;
for(int i = 0; i < 3; i++)
sum += a[i][j];
[Link]("Column sum = " + sum);
}
1️⃣ 3️⃣ Diagonal Sums
1️⃣3️⃣
🔹 Left Diagonal
Condition: i == j
int sum = 0;
for(int i = 0; i < 3; i++)
sum += a[i][i];
🔹 Right Diagonal
Condition: i + j == n - 1
int sum = 0;
for(int i = 0; i < 3; i++)
sum += a[i][2 - i];
📝 EXAM TIPS (VERY IMPORTANT)
✔ Sorting & searching → only 1D array
✔ Binary search → array must be sorted
✔ Use length instead of fixed size
✔ Matrix display must be proper
✔ Diagonal logic asked frequently