Java 实例 - 数组扩容

以下实例演示了如何在数组初始化后对数组进行扩容:

Main.java 文件

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

以上代码运行输出结果为:

  1. A
  2. B
  3. C
  4. D
  5. E