Array Declaration and Initialization in DSA Java - Time & Space Complexity
When we create and fill an array, it takes some time depending on how many items we add.
We want to know how the time needed grows as the array size grows.
Analyze the time complexity of the following code snippet.
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i * 2;
}
This code creates an array of size n and fills it with values by multiplying the index by 2.
- Primary operation: The for-loop that assigns values to each array element.
- How many times: Exactly n times, once for each element in the array.
As the array size grows, the number of assignments grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The time grows directly in proportion to the size of the array.
Time Complexity: O(n)
This means the time to create and fill the array grows linearly with the number of elements.
[X] Wrong: "Creating an array and filling it is instant, no matter the size."
[OK] Correct: Each element must be assigned a value, so the time grows with the number of elements.
Understanding how array initialization time grows helps you explain performance when working with large data sets.
"What if we initialize the array without a loop, using default values? How would the time complexity change?"
Practice
Solution
Step 1: Understand Java array declaration syntax
In Java, arrays are declared with the type followed by square brackets and initialized with the new keyword and size inside square brackets.Step 2: Check each option for correct syntax
int[] arr = new int[5]; uses correct syntax: int[] arr = new int[5]; Options A, B, and D have syntax errors or missing keywords.Final Answer:
int[] arr = new int[5]; -> Option AQuick Check:
Correct Java array declaration = int[] arr = new int[5]; [OK]
- Forgetting 'new' keyword
- Using wrong brackets or missing type
- Declaring array without brackets
Solution
Step 1: Understand array initialization with values
In Java, to initialize an array with values using 'new', syntax is: int[] arr = new int[] {values};Step 2: Check each option
int[] arr = new int[] {1, 2, 3, 4};: correct syntax. int arr[] = new int{1, 2, 3, 4};: incorrect declaration style and missing [] after new int. int[] arr = new int{1, 2, 3, 4};: missing [] after new int. int arr = {1, 2, 3, 4};: arr not declared as array.Final Answer:
int[] arr = new int[] {1, 2, 3, 4}; -> Option AQuick Check:
Array init with 'new int[]' = int[] arr = new int[] {1, 2, 3, 4}; [OK]
- Omitting 'new' when required
- Using wrong brackets
- Assigning values without array type
int[] arr = new int[3]; arr[0] = 5; arr[2] = 10; System.out.println(arr[1]);
Solution
Step 1: Understand default values in integer arrays
When an integer array is created with 'new int[3]', all elements are initialized to 0 by default.Step 2: Check assigned values and output
arr[0] = 5 and arr[2] = 10 are assigned. arr[1] is not assigned, so it remains 0. The print statement outputs arr[1], which is 0.Final Answer:
0 -> Option BQuick Check:
Unassigned int array element = 0 [OK]
- Assuming unassigned elements have garbage values
- Confusing index positions
- Expecting runtime error for unassigned index
int arr[5];
Solution
Step 1: Understand Java array declaration rules
In Java, you cannot specify the size of the array in the declaration alone. Size must be specified during initialization with 'new'.Step 2: Analyze the given code
int arr[5]; tries to declare an array with size 5 without initialization, which is invalid syntax in Java.Final Answer:
Array size cannot be specified in declaration without initialization -> Option CQuick Check:
Size only with 'new' keyword [OK]
- Using C-style array declaration
- Trying to declare size without 'new'
- Confusing declaration and initialization
int[] arr = {1, 2, 3};
arr = new int[] {4, 5};
System.out.print(arr.length);Choose the correct output and explain why.
Solution
Step 1: Understand array reassignment
Initially, arr points to an array of length 3 with values {1, 2, 3}. Then arr is reassigned to a new array with values {4, 5} of length 2.Step 2: Check the length property after reassignment
After reassignment, arr.length reflects the new array length, which is 2.Final Answer:
2 -> Option DQuick Check:
Array length after reassignment = 2 [OK]
- Assuming original array length remains
- Expecting error on reassignment
- Confusing array contents with length
Ready to prove it?
Pass the test and this pattern moves unknown
