Java 實(shí)例 - 數(shù)組擴(kuò)容

Java 實(shí)例 Java 實(shí)例

以下實(shí)例演示了如何在數(shù)組初始化后對(duì)數(shù)組進(jìn)行擴(kuò)容:

/*
 author by w3cschool.cn
 文件名:Main.java
 */

public class Main {
   public static void main(String[] args) {
      String[] names = new String[] { "A", "B", "C" };
      String[] extended = new String[5];
      extended[3] = "D";
      extended[4] = "E";
      System.arraycopy(names, 0, extended, 0, names.length);
      for (String str : extended){
         System.out.println(str);
      }
   }
}

以上代碼運(yùn)行輸出結(jié)果為:

A
B
C
D
E

Java 實(shí)例 Java 實(shí)例