DMCA.com Protection Status Java Programming Lesson 6 Part-1 | Arrays in Java Explained - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 6 Part-1 | Arrays in Java Explained

LESSON-6: ARRAYS (1D & 2D)


📘 Lesson 6 — Arrays in Java


✅ What is an Array?

An array is a collection of multiple values stored in one variable.

Example:

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

📌 Arrays store same data type
📌 Index starts from 0
📌 Size is fixed


✅ 1️⃣ One-Dimensional (1D) Array

✅ Declaration Methods

int arr[] = {10, 20, 30, 40}; // Method 1 int arr2[] = new int[5]; // Method 2 arr2[0] = 5; // Assign later

✅ Loop through Array

public class ArrayExample1D { public static void main(String[] args) { int marks[] = {85, 90, 75, 88, 92}; for(int i = 0; i < marks.length; i++){ System.out.println("Marks[" + i + "]: " + marks[i]); } } }

marks.length = array size


✅ Enhanced for-loop (foreach loop)

for(int x : marks){ System.out.println(x); }

✅ 2️⃣ Two-Dimensional (2D) Array (Matrix Form)

Arrays inside arrays → rows & columns

int numbers[][] = { {1, 2, 3}, {4, 5, 6} };

✅ numbers has → 2 rows, each row 3 elements


✅ Print 2D Array

public class ArrayExample2D { public static void main(String[] args) { int matrix[][] = { {10, 20, 30}, {40, 50, 60} }; for(int i = 0; i < 2; i++){ for(int j = 0; j < 3; j++){ System.out.print(matrix[i][j] + " "); } System.out.println(); } } }

✅ Output:

10 20 30 40 50 60

✅ Input Array from User (Important ✅)

import java.util.Scanner; public class UserArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arr[] = new int[5]; System.out.println("Enter 5 numbers:"); for(int i = 0; i < 5; i++){ arr[i] = sc.nextInt(); } System.out.println("You entered:"); for(int x : arr){ System.out.print(x + " "); } } }

✅ Common Array Mistakes

MistakeIssue
Accessing arr[5] when size is 5❌ Index out of bounds (0–4 valid)
Mixed data types in same array❌ Not allowed
Forgetting array size is fixed❌ Cannot expand

📝 Exercise for You ✅ (Do it now)

Write 2 programs:

1️⃣ 1D Array → Store 5 integers and print the sum
2️⃣ 2D Array → Print this pattern using array:

1 2 3 4 5 6 7 8 9

Reply with:

✅ Your Code
✅ Output

I will check and give feedback ✅🔥


🎯 Quick Quiz

1️⃣ Array index always starts from:
A) 0
B) 1
C) -1

2️⃣ Which gets array size?
A) size()
B) count()
C) length

Reply format:
1 → ?, 2 → ?


✅ Lesson-6 Completed!

Next → What should we learn?


➡ NEXT Lesson-6 Part-2: Strings (String class & methods)

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.