Bird
Raised Fist0
Javaprogramming~10 mins

Common array operations in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Common array operations
Start
↓
Create array
↓
Access elements by index
↓
Modify elements by index
↓
Traverse array with loop
↓
Search or process elements
↓
End
This flow shows how we create an array, access or change its elements by index, loop through it, and perform operations like searching or processing.
Execution Sample
Java
int[] arr = {10, 20, 30};
arr[1] = 25;
for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i] + " ");
}
This code creates an array, changes the second element, then prints all elements.
Execution Table
StepActionIndex/VariableValue/ConditionArray StateOutput
1Create array--[10, 20, 30]-
2Modify elementarr[1]25[10, 25, 30]-
3Start loopi=00 < 3 is true[10, 25, 30]-
4Print elementarr[0]10[10, 25, 30]10
5Increment ii=1-[10, 25, 30]10
6Check loopi=11 < 3 is true[10, 25, 30]10
7Print elementarr[1]25[10, 25, 30]10 25
8Increment ii=2-[10, 25, 30]10 25
9Check loopi=22 < 3 is true[10, 25, 30]10 25
10Print elementarr[2]30[10, 25, 30]10 25 30
11Increment ii=3-[10, 25, 30]10 25 30
12Check loopi=33 < 3 is false[10, 25, 30]10 25 30
13End loop--[10, 25, 30]10 25 30
💡 Loop ends when i reaches 3 and condition i < arr.length is false
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 11Final
arr[10, 20, 30][10, 25, 30][10, 25, 30][10, 25, 30][10, 25, 30][10, 25, 30]
i--1233
Key Moments - 3 Insights
Why does the loop stop when i equals 3 and not when i is greater than 3?
Because the loop condition is i < arr.length (3), so when i becomes 3, the condition is false and the loop stops, as shown in steps 12 and 13.
What happens if we try to access arr[3]?
Accessing arr[3] would cause an error because valid indices are 0 to 2. The array length is 3, so index 3 is out of bounds.
Why do we use arr.length in the loop condition?
Using arr.length ensures the loop runs exactly for all valid indices, preventing out-of-bounds errors, as seen in the condition checks in steps 3, 6, 9, and 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr after step 2?
A[10, 20, 30]
B[25, 20, 30]
C[10, 25, 30]
D[10, 30, 25]
💡 Hint
Check the 'Array State' column at step 2 in the execution table.
At which step does the loop condition become false?
AStep 11
BStep 12
CStep 13
DStep 10
💡 Hint
Look at the 'Value/Condition' column for the loop check steps in the execution table.
If we change arr[1] = 25 to arr[1] = 50, what will be printed at step 7?
A50
B25
C20
D30
💡 Hint
Refer to the 'Action' and 'Output' columns at step 7 in the execution table.
Concept Snapshot
Common array operations in Java:
- Create with int[] arr = {10, 20, 30};
- Access/modify elements by index: arr[1] = 25;
- Loop with for (int i = 0; i < arr.length; i++)
- Use arr.length to avoid out-of-bounds
- Arrays have fixed size after creation
Full Transcript
This lesson shows common array operations in Java. We start by creating an array with three numbers. Then we change the second element from 20 to 25. Next, we use a for loop to go through each element by index and print it. The loop runs while the index is less than the array length, which is 3. When the index reaches 3, the loop stops. We track the array and index values step by step. Key points include using arr.length to avoid errors and understanding when the loop ends. The quiz checks understanding of array state and loop behavior.

Practice

(1/5)
1. Which of the following correctly declares an array of integers with 5 elements in Java?
easy
A. int[] numbers = int[5];
B. int numbers = new int[5];
C. int numbers[] = new int;
D. int[] numbers = new int[5];

Solution

  1. Step 1: Understand Java array declaration syntax

    In Java, arrays are declared with the type followed by square brackets, then the variable name, and initialized with 'new' and size.
  2. Step 2: Check each option for correct syntax

    int[] numbers = new int[5]; uses correct syntax: int[] numbers = new int[5]; Options A, B, and D have syntax errors or missing parts.
  3. Final Answer:

    int[] numbers = new int[5]; -> Option D
  4. Quick Check:

    Correct array declaration = int[] numbers = new int[5]; [OK]
Hint: Remember: type[] name = new type[size]; for arrays [OK]
Common Mistakes:
  • Missing square brackets after type
  • Not using 'new' keyword
  • Incorrect variable initialization
2. Which of the following is the correct way to access the third element of an array named arr in Java?
easy
A. arr(3);
B. arr[2];
C. arr[3];
D. arr.get(2);

Solution

  1. Step 1: Recall Java array indexing

    Java arrays use zero-based indexing, so the first element is at index 0, second at 1, third at 2.
  2. Step 2: Identify correct syntax for accessing elements

    Elements are accessed with square brackets []. So arr[2] accesses the third element. Options A and D use incorrect syntax, and C accesses the fourth element.
  3. Final Answer:

    arr[2]; -> Option B
  4. Quick Check:

    Third element index = 2 [OK]
Hint: Arrays start at 0, so third element is arr[2] [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using 1-based indexing
  • Trying to use methods like get() on arrays
3. What is the output of the following Java code?
int[] nums = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < nums.length; i++) {
    sum += nums[i];
}
System.out.println(sum);
medium
A. 15
B. 10
C. 5
D. 0

Solution

  1. Step 1: Understand the array and loop

    The array nums contains 1, 2, 3, 4, 5. The loop runs from i=0 to i=4 (nums.length is 5).
  2. Step 2: Calculate the sum of all elements

    sum = 0 + 1 + 2 + 3 + 4 + 5 = 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Sum of 1 to 5 = 15 [OK]
Hint: Sum array by adding all elements in a loop [OK]
Common Mistakes:
  • Off-by-one error in loop
  • Starting sum at wrong value
  • Using wrong loop condition
4. The following code is intended to print all elements of the array data, but it causes a runtime error. What is the error?
int[] data = {10, 20, 30};
for (int i = 1; i <= data.length; i++) {
    System.out.println(data[i]);
}
medium
A. ArrayIndexOutOfBoundsException because loop goes beyond last index
B. Syntax error due to wrong for loop syntax
C. Prints all elements correctly
D. NullPointerException because array is not initialized

Solution

  1. Step 1: Analyze loop indices and array length

    The array has length 3, valid indices are 0, 1, 2. The loop runs i from 1 to 3 inclusive.
  2. Step 2: Identify index causing error

    When i = 3, data[3] is accessed, which is out of bounds and causes ArrayIndexOutOfBoundsException.
  3. Final Answer:

    ArrayIndexOutOfBoundsException because loop goes beyond last index -> Option A
  4. Quick Check:

    Loop index must be less than length [OK]
Hint: Loop indices must be 0 to length-1 for arrays [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Starting loop at 1 instead of 0
  • Ignoring zero-based indexing
5. Given an integer array arr, which code snippet correctly counts how many elements are greater than 10?
hard
A. int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] >= 10) { count++; } } System.out.println(count);
B. int count = 0; for (int i = 1; i <= arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count);
C. int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count);
D. int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] < 10) { count++; } } System.out.println(count);

Solution

  1. Step 1: Understand the counting condition

    We want to count elements strictly greater than 10, so condition is arr[i] > 10.
  2. Step 2: Check loop indices and condition correctness

    int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count); uses correct zero-based indexing and correct condition. int count = 0; for (int i = 1; i <= arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count); has out-of-bounds error. int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] >= 10) { count++; } } System.out.println(count); counts elements >= 10 (includes 10). int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] < 10) { count++; } } System.out.println(count); counts elements less than 10, which is wrong.
  3. Final Answer:

    int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count); -> Option C
  4. Quick Check:

    Count elements with arr[i] > 10 using correct loop [OK]
Hint: Use i < arr.length and condition arr[i] > 10 to count [OK]
Common Mistakes:
  • Using <= instead of < in loop
  • Using wrong comparison operator
  • Starting loop at 1 causing index errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard