📖 Scenario: You are managing a small inventory of fruits in a basket. Each fruit has a quantity stored in an array. You want to check the quantity of a specific fruit and then update it when you add more fruits.
🎯 Goal: Learn how to access and update elements in an array by their index in Java.
📋 What You'll Learn
Create an integer array called fruitQuantities with exact values: 5, 8, 12, 3, 7
Create an integer variable called indexToUpdate and set it to 2
Update the element at indexToUpdate in fruitQuantities by adding 4 to its current value
Print the updated fruitQuantities array elements separated by spaces
💡 Why This Matters
🌍 Real World
Managing quantities of items in a list, like inventory counts or scores.
💼 Career
Understanding array access and update is fundamental for programming tasks involving lists and collections.
Progress0 / 4 steps
1
Create the fruit quantities array
Create an integer array called fruitQuantities with these exact values: 5, 8, 12, 3, 7
DSA Java
Hint
Use curly braces to list the values inside the array declaration.
2
Set the index to update
Create an integer variable called indexToUpdate and set it to 2
DSA Java
Hint
Use int indexToUpdate = 2; to create the variable.
3
Update the quantity at the given index
Add 4 to the element at indexToUpdate in the fruitQuantities array
DSA Java
Hint
Use fruitQuantities[indexToUpdate] += 4; to add 4 to the current value.
4
Print the updated array
Print all elements of the fruitQuantities array separated by spaces using a for loop with variable i
DSA Java
Hint
Use a for loop from 0 to fruitQuantities.length and print each element followed by a space.
Practice
(1/5)
1. What does the expression arr[2] do in a Java array named arr?
easy
A. Deletes the element at index 2
B. Adds a new element at index 2
C. Accesses the element at index 2 of the array
D. Returns the length of the array
Solution
Step 1: Understand array indexing
In Java, arrays use zero-based indexing, so arr[2] accesses the third element.
Step 2: Identify the operation
The expression reads the value stored at index 2 without modifying the array.
Final Answer:
Accesses the element at index 2 of the array -> Option C
Quick Check:
arr[2] means get element at index 2 [OK]
Hint: Remember arrays start at index 0, so arr[2] is third element [OK]
Common Mistakes:
Thinking arr[2] adds or deletes elements
Confusing index with array length
Assuming indexing starts at 1
2. Which of the following is the correct syntax to update the element at index 1 of an integer array nums to 10?
easy
A. nums(1) = 10;
B. nums[1] = 10;
C. nums{1} = 10;
D. nums.1 = 10;
Solution
Step 1: Recall Java array update syntax
In Java, array elements are accessed and updated using square brackets: array[index] = value;
Step 2: Match correct syntax
Only nums[1] = 10; uses correct brackets and assignment.
Final Answer:
nums[1] = 10; -> Option B
Quick Check:
Use square brackets for array index update [OK]
Hint: Use square brackets [] to access or update array elements [OK]
Common Mistakes:
Using parentheses () instead of brackets []
Using curly braces {} which are invalid for arrays
Trying to access with dot notation
3. What will be the output of the following Java code?