Array Basics & Iteration
Understand array basics: fixed-size, zero-based indexing, create/initialize, and iterate safely.
Array Basics & Iteration is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is an array
Array is a fixed-size sequence of elements of the same type, stored under one name and indexed from 0.
- Use when you know the element count
- Access by index: first is 0
- Type-safe: all items have the same type
Declare & Create
Declare and create arrays:
public class Main {
public static void main(String[] args) {
// Way 1: Declare an array with fixed size
int[] a = new int[3];
// All elements are 0 by default: [0, 0, 0]
// Way 2: Declare and initialize in one line
int[] b = {10, 20, 30};
// This array already has values: [10, 20, 30]
// Print both arrays
System.out.print("Array a: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(); // new line
System.out.print("Array b: ");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
Access & Update
Access and update by index:
Valid indices for 3 elements: 0, 1, 2.
public class Main {
public static void main(String[] args) {
// Create and initialize an array
int[] a = {5, 7, 9}; // array has 3 elements: [5, 7, 9]
// Access the first element (index 0)
int first = a[0]; // first = 5
System.out.println("First element: " + first);
// Update the second element (index 1)
a[1] = 8; // now array is [5, 8, 9]
// Print the whole array to see the update
System.out.print("Updated array: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
Length & For
Use a.length to get the element count. Iterate with a for loop:
public class Main {
public static void main(String[] args) {
// Example array
int[] a = {5, 8, 9}; // 3 elements
int sum = 0; // start with 0
// Loop through each element in the array
for (int i = 0; i < a.length; i = i + 1) {
// Add the current element to sum
sum = sum + a[i];
}
// Print the final result
System.out.println("Sum of array elements = " + sum);
}
}
For-each
Enhanced for-each reads elements without index:
int total = 0;
for (int v : b) {
total = total + v;
}
Use when you do not need the index.
Array Demo
Run it: The demo prints the length, sum, max, and every element using for and for-each.
public class Main {
static int sum(int[] arr) {
int s = 0;
for (int i = 0; i < arr.length; i = i + 1) {
s = s + arr[i];
}
return s;
}
static int max(int[] arr) {
int m = arr[0];
for (int i = 1; i < arr.length; i = i + 1) {
if (arr[i] > m) m = arr[i];
}
return m;
}
public static void main(String[] args) {
int[] nums = {3, 1, 4, 1, 5};
System.out.println("Length = " + nums.length);
System.out.println("Sum = " + sum(nums));
System.out.println("Max = " + max(nums));
System.out.println("Elements:");
for (int v : nums) {
System.out.println(v);
}
}
}
Array Length
Quick check: How do you get the number of elements in array a?
Recap & Next
Recap: You created arrays, accessed and updated items, used length, and iterated with for and for-each.
Next: Learn common array algorithms like search and reverse.
Frequently asked questions
Is the “Array Basics & Iteration” lesson free?
Yes — the full text of “Array Basics & Iteration” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Array Basics & Iteration”?
Understand array basics: fixed-size, zero-based indexing, create/initialize, and iterate safely. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Array Basics & Iteration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Array Basics & Iteration
- Searching, Min/Max & Reverse
- Insert, Delete by Index & Shift