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 named ages. The square brackets indicate that ages is an array.

  • Initialization: The statement ages = new int[127]; initializes the array ages with 127 elements. The new keyword 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.
Correct Statement for Array Initialization in Java

原文地址: https://www.cveoy.top/t/topic/oAPz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录