Java Array Fundamentals: Syntax, Indexing, and Code Examples
Java Array Fundamentals: Core Concepts and Syntax
Array Properties and Declaration
Fixed Size Property
Once an array is created, its size is fixed.
Valid Array Declarations (int values)
Which of the following are correct ways to declare an array of int
values?
int[] a;
int a[];
Valid Array Declarations (Mixed Types)
Which of the following statements are valid?
double d[] = new double[30];
int[] i = {3, 4, 3, 2};
Incorrect Array Declarations and Initializations
Which of the following declarations or initializations are incorrect?
int[] a = new int(2);
(Incorrect: Use brackets[]
for size.)int a = new int[2];
(Incorrect: Type mismatch;a
isint
, notint[]
.)int a() = new int[2];
(Incorrect: Syntax error.)
Character Array Initialization Methods
How can you initialize an array of two characters to ‘a’ and ‘b’?
Both methods below are valid:
char[] charArray = {'a', 'b'};
char[] charArray = new char[2]; charArray[0] = 'a'; charArray[1] = 'b';
Array Size and Indexing
Determining Array Bounds
If you declare an array double[] list = new double[5]
:
- How many elements are in the array? 5
- The highest index in array
list
is 4.
Accessing Elements by Index
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}
, what is list[1]
?
Answer: 2.0
Code Analysis: Array Manipulation and Errors
Finding the Index of the Maximum Value
Analyze the following code snippet designed to find the index of the maximum element. What is the output?
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
Output: 1 (The code finds the index of the first occurrence of the maximum value.)
Runtime Error: ArrayIndexOutOfBoundsException
Analyze the following code:
public class Test {
public static void main(String[] args) {
int[] x = new int[5];
int i;
for (i = 0; i < x.length; i++)
x[i] = i;
System.out.println(x[i]);
}
}
Result: The program has a runtime error because the last statement System.out.println(x[i]);
causes an ArrayIndexOutOfBoundsException
. The loop terminates when i
equals 5, which is outside the valid index range [0, 4].
Enhanced For Loop Output
Analyze the following code using an enhanced for
loop:
public class Test {
public static void main(String[] args) {
double[] x = {2.5, 3, 4};
for (double value: x)
System.out.print(value + " ");
}
}
Output: The program displays 2.5 3.0 4.0
.
Array Shifting and Value Propagation
What is the output of the following code?
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
Output: 1 1 1 1 1 1
Reference Assignment and Shared Memory
In the following code, what is the output for list2
?
public class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1; // list2 now points to the same array as list1
list1[0] = 0;
list1[1] = 1;
list2[2] = 2; // Modifies the shared array
for (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + " ");
}
}
Output: 0 1 2
The Final Keyword and Array References
Analyze the following code:
public class Test {
public static void main(String[] args) {
final int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2]; // Attempt to reassign the final reference x
for (int i = 0; i < y.length; i++)
System.out.print(y[i] + " ");
}
}
Result: The program has a compile error on the statement x = new int[2]
, because x
is declared as final
and its reference cannot be changed.
Miscellaneous Code Observations
Unidentified Code Output 1
What is the output of the following code? int 2 3 4 5 6 1.
(Note: This is a standalone output fragment, possibly resulting from an array rotation operation.)
Unidentified Code Output 2
Analyze the following code: The program displays 1 2 3 4
.
Fragmented Code Snippet
Analyze the following code. public (int)(Math.random() * 100)) c). i + 10 |
(This snippet is highly fragmented and lacks context, likely related to generating random array values.)