[OCAJP]for迴圈4

Given the code fragment:

And given the requirements:

Process all the elements of the array in the order of entry.
Process all the elements of the array in the reverse order of entry.
Process alternating elements of the array in the order of entry.

Which two statements are true?

A. Requirements 1, 2, and 3 can be implemented by using the enhanced for loop.
B. Requirements 1, 2, and 3 can be implemented by using the standard for loop.
C. Requirements 2 and 3 CANNOT be implemented by using the standard for loop.
D. Requirement 1 can be implemented by using the enhanced for loop.
E. Requirement 3 CANNOT be implemented by using either the enhanced for loop or the standard for loop.

題解

需求1,按照順序走訪array陣列物件可以使用標準的for loop或是增強的for loop(即foreach)來實作出來。程式如下:

需求2,反向走訪array陣列物件只可以使用標準的for loop來實作。

需求3,使用標準的for loop,可以間格走訪array陣列物件,實作方式如下:

所以選項B和選項D的敘述是正確的。

關於作者

Magic Len

各位好,我是Magic Len,是這網站的管理員。我是台灣台中大肚山上人,畢業於台中高工資訊科和台灣科技大學資訊工程系,曾在桃機航警局服役。我熱愛自然也熱愛科學,喜歡和別人分享自己的知識與經驗。如果你有興趣認識我,可以加我的Facebook,並且請註明是從MagicLen來的。

相關文章