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
✅ What is an Array?
An array is a collection of multiple values stored in one variable.
Example:
📌 Arrays store same data type
📌 Index starts from 0
📌 Size is fixed
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
2️⃣ Declaration + Memory Allocation
3️⃣ Declaration + Initialization Together
🔷 Accessing Array Elements
Use the index number:
⚠ Index starts from 0 and ends at length - 1
✅ 1️⃣ One-Dimensional (1D) Array
✅ Declaration Methods
✅ Loop through Array
✔ marks.length = array size
✅ Enhanced for-loop (foreach loop)
✅ 2️⃣ Two-Dimensional (2D) Array (Matrix Form)
Arrays inside arrays → rows & columns
✅ numbers has → 2 rows, each row 3 elements
✅ Print 2D Array
✅ Output:
✅ Input Array from User (Important ✅)
✅ Common Array Mistakes
| Mistake | Issue |
|---|---|
| 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
🔷 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:
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.