從 Java 中的方法返回一個數組


方法也可能返回一個數組。例如,以下方法返回一個數組,該陣列是另一個數組的反轉 -

示例

public static int[] reverse(int[] list) {
   int[] result = new int[list.length];
   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
      result[j] = list[i];
   }
   return result;
}

更新於: 25-Feb-2020

218 次瀏覽

開啟您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.