📜  如何在Java中初始化后扩展数组?

📅  最后修改于: 2022-05-13 01:55:08.755000             🧑  作者: Mango

如何在Java中初始化后扩展数组?

在Java中,数组是不可变的,即如果数组一旦被分配或实例化,则为数组分配的内存不能减少或增加。但是有一种形式的解决方案,我们可以在其中扩展数组。

初始化后扩展数组:由于声明数组后不能修改数组大小,只能通过初始化一个新数组并将旧数组的值复制到新数组中来扩展它,然后赋值根据声明的数组的大小为数组添加新值。

下面是显示初始化后扩展数组的示例。

示例 1:

Java
// java program to demonstrate
// extending an array
import java.lang.*;
 
class ExtendingArray {
   
    public static void main(String[] args)
    {
 
        // initializing string array
        String[] words = new String[] { "G", "E", "E" };
 
        // allocating space for 5 strings
        // in the extended array
        String[] extendWords = new String[5];
 
        // adding new string
        // at index 3 and 4
        extendWords[3] = "K";
        extendWords[4] = "S";
 
        // copying the array elements
        // to new extended array
        System.arraycopy(words, 0, extendWords, 0,
                         words.length);
 
        // printing the extended array
        // elements
        for (String str : extendWords) {
            System.out.print(str);
        }
    }
}


Java
// Java program to demonstrate
// extending an array
 
import java.lang.*;
 
class ExtendingArray {
   
    public static void extendedArray()
    {
 
        // initializing integers to array int
        int[] num = new int[] { 1, 2, 3, 4, 5, 6 };
 
        // allocating space for 10 integers
        int[] extendnum = new int[10];
 
        // adding new integers
        // at index 6,7,8,9
        extendnum[6] = 7;
        extendnum[7] = 8;
        extendnum[8] = 9;
        extendnum[9] = 10;
 
        // copying old array to new array
        System.arraycopy(num, 0, extendnum, 0, num.length);
 
        // print the elements of
        // extended array using for-each
        for (int str : extendnum)
            System.out.println(str);
    }
    public static void main(String[] args)
    {
 
        // create an instance
        ExtendingArray exarr = new ExtendingArray();
 
        // extend an array and print them
        exarr.extendedArray();
    }
}


输出
GEEKS

示例 2:

Java

// Java program to demonstrate
// extending an array
 
import java.lang.*;
 
class ExtendingArray {
   
    public static void extendedArray()
    {
 
        // initializing integers to array int
        int[] num = new int[] { 1, 2, 3, 4, 5, 6 };
 
        // allocating space for 10 integers
        int[] extendnum = new int[10];
 
        // adding new integers
        // at index 6,7,8,9
        extendnum[6] = 7;
        extendnum[7] = 8;
        extendnum[8] = 9;
        extendnum[9] = 10;
 
        // copying old array to new array
        System.arraycopy(num, 0, extendnum, 0, num.length);
 
        // print the elements of
        // extended array using for-each
        for (int str : extendnum)
            System.out.println(str);
    }
    public static void main(String[] args)
    {
 
        // create an instance
        ExtendingArray exarr = new ExtendingArray();
 
        // extend an array and print them
        exarr.extendedArray();
    }
}
输出
1
2
3
4
5
6
7
8
9
10