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)



As your Java programs grow, you’ll often need to store and manage multiple values at once. Instead of creating separate variables for each value, Java provides a powerful structure called an Array.

In this lesson, we will explore:

  • What arrays are
  • Why they are useful
  • Types of arrays
  • How to create, store, and access elements
  • Common mistakes and best practices
  • Practical code examples for beginners

📘 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


🔷 Why Use Arrays?

int s1 = 50, s2 = 60, s3 = 70, s4 = 80;
int[] scores = {50, 60, 70, 80};

Arrays help you:

  • Store large sets of data easily
  • Process lists using loops
  • Manage related values under a single variable
  • Increase readability and performance

Without arrays, you would need multiple variables like:

int s1 = 50, s2 = 60, s3 = 70, s4 = 80;

With an array:

int[] scores = {50, 60, 70, 80};

Much cleaner ✔

🔷 Declaring and Initializing Arrays

1️⃣ Declaration Only

int[] marks;

2️⃣ Declaration + Memory Allocation

marks = new int[5]; // stores 5 integers

3️⃣ Declaration + Initialization Together

int[] marks = {90, 85, 70, 88, 95};

🔷 Accessing Array Elements

Use the index number:

System.out.println(marks[0]); // First element System.out.println(marks[4]); // Last element

⚠ Index starts from 0 and ends at length - 1

✅ 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

🔷 Practical Example: Taking Input Using Scanner

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


🔷 Real-Life Uses of Arrays

Arrays are used in:

  • Student marks storage
  • Product lists
  • Storing user inputs
  • Gaming (positions, scores)
  • Storing database results
  • Handling sequences of numbers or text


🔷 Summary of What You Learned

In this lesson, you understood:

✔ What arrays are
✔ Why arrays are important
✔ How to declare, initialize & modify arrays
✔ How to loop through arrays
✔ 1D, 2D, and multidimensional arrays
✔ Common errors beginners make
✔ How to accept array input using Scanner

You now have a strong foundation in Arrays — a key building block of Java programming.

📝 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)


Frequently Asked Questions (FAQs)

What is an array in Java?

An array in Java is a data structure that stores multiple values of the same data type in a single variable. It allows indexed access to each element starting from index 0.

How do you declare an array in Java?

Arrays are declared using the data type followed by square brackets, for example: int[] numbers;. This only creates a reference, not the actual array.

How do you initialize an array in Java?

Arrays can be initialized in two ways: 1) Using the new keyword: int[] nums = new int[5]; 2) Using array literals: int[] nums = {10, 20, 30};.

What is the length of an array?

The length of an array refers to the number of elements it can hold. You can get it using arrayName.length.

Can arrays store different data types?

No. Arrays in Java are homogeneous, meaning they store elements of only one data type. If you need mixed types, use collections like ArrayList<Object>.

What is an ArrayIndexOutOfBoundsException?

This exception occurs when you try to access an index that does not exist in the array (e.g., accessing index 5 in an array of size 5).

How can you loop through an array?

You can use a traditional for loop, an enhanced for-each loop, or Java Streams to iterate through an array.

What is the difference between 1D and 2D arrays?

A 1D array stores values in a single row, while a 2D array is like a table with rows and columns. Example: int[][] matrix = new int[3][3];

No comments

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

Theme images by 5ugarless. Powered by Blogger.