Correct Statement for Array Initialization in Java
Which of the following statements about operations on arrays is correct?
A) int ages[]; ages = new int[127];
B) int ages[]; ages[] = new int[12];
C) int ages[]; ages = (1, 2, 3);
D) int ages; ages[7] = new int[12];
Correct Answer:
A) int ages[]; ages = new int[127];
Explanation:
-
Declaration: The statement
int ages[];declares an integer array namedages. The square brackets indicate thatagesis an array. -
Initialization: The statement
ages = new int[127];initializes the arrayageswith 127 elements. Thenewkeyword creates a new array object in memory, and the size of the array (127) is specified within the square brackets.
Why Other Options are Incorrect:
- B)
ages[] = new int[12];The syntax is incorrect. You don't use square brackets after the array name when assigning a new array to it. - C)
ages = (1, 2, 3);This syntax is incorrect. You can't directly assign values to an array using parentheses. You need to use an initializer list or assign values individually. - D)
ages[7] = new int[12];This is incorrect. You can't assign an array to a single element of another array. Array elements must be individual values of the same data type as the array itself.
原文地址: https://www.cveoy.top/t/topic/oAPz 著作权归作者所有。请勿转载和采集!