JAVA一維數組的複製、插入、合併

JAVA一維數組的複製、插入、合併

package JAVA_Project_01_04;//創建一個包
//數組是一種重要的數據結構。一個數組是具有同一個數據類型的對象的集合。數據類型可以是簡單類型,也可以是類。
//數組可以是一維的,也可以是多維的,通過其下標可以訪問數組元素。數組提供了一種將有聯繫的信息進行分組的有效方法。
import java.util.Scanner;//導入類
/*本質上講,數組是一組相關的存儲單元,這些存儲單元在邏輯上被看作是相互獨立的若干元素,它們具有相同的名字和數據類型。
數組中的元素位置由它們的下標決定。對數組的操作包括數組的複製,數組的合併,插入以及搜索等。
 */
public class TextOperatorArray {//對數組元素進行各種操作的類

    public static void copy() {//數組複製
        int array[] = new int[]{1, 2, 3, 4};//聲明數組並初始化
        int temp1[] = new int[array.length];//聲明數組長度爲array數組長度
        int temp2[] = new int[array.length];
        System.arraycopy(array, 0, temp1, 0, array.length);//將數組array複製給數組temp1
        System.out.print("複製後的數組結果:");
        for (int i = 0; i < temp1.length; i++)//循環顯示數組元素
            System.out.print(temp1[i] + "");
        System.out.println();
        temp2 = array;//使用賦值將數組array複製給數組temp2
    }

    public static void insert() {//數組插入
        int i, j;
        int n = 5;
        int num[] = new int[n + 1];
        for (i = 0; i < num.length - 1; i++) {
            num[i] = (i + 1) * 6;
        }
        int length = num.length;//獲得數組長度
        System.out.println("插入數字之前的數組爲:");
        for (i = 0; i < length; i++)//循環顯示數組元素
            if (num[i] == 0)
                System.out.print("存儲空間");
            else
                System.out.println();
        System.out.println();
        System.out.println("輸出一個要插入的數:");
        Scanner scan = new Scanner(System.in);//鍵盤輸入一個數
        int in = scan.nextInt();
        for (i = 0; i < length - 1; i++) {//循環查找一個大於要插入的數的位置
            if (num[i] > in)//找到大於要插入的數就跳出
                break;
        }
        for (j = length - 1; j > i; j--) {//爲要插入的數留出位置
            num[j] = num[j - 1];
        }
        num[j] = in;//將要插入的數保存到該位置
        for (i = 0; i < length; i++)//循環顯示數組元素
            System.out.print(num[i] + "");
        System.out.println();
    }

    public static int[] combine(int[] a, int[] b) {//數組合並並排好序
        int alen = a.length;//獲得傳入的數組的長度
        int blen = b.length;
        int length = alen + blen;
        int i, j;
        System.out.println("合併之間的兩個數組:");
        for (i = 0; i < alen; i++)//循環顯示a數組元素
            System.out.print(a[i] + "");
        System.out.println();
        for (i = 0; i < blen; i++)//循環顯示b數組元素
            System.out.print(b[i] + "");
        System.out.println();
        int[] c = new int[length];//聲明數組長度爲傳入兩個數組長度之和
        for (i = 0, j = 0; i < alen && j < blen; ) {//循環查看元素進行比較
            if (a[i] < b[j]) {//判斷兩個數組元素值的大小
                c[i + j] = a[i];
                i++;
            } else {
                c[i + j] = b[j];
                j++;
            }
        }
        if (i == alen)//將b數組從下標爲i開始將值賦給c數組,放在從c數組的blen+i,alen-i之間
            System.arraycopy(b, j, c, alen + j, blen - j);
        if (j == blen)
            System.arraycopy(a, i, c, blen + i, alen - i);
        System.out.println("合併並排序之後的新數組:");
        for (int k = 0; k < a.length + b.length; k++)//循環輸出合併後的數組的元素
            System.out.print(c[k] + "");
        System.out.println();
        return c;
    }

    public static void main(String[] args) {//Java程序的主入口處
        System.out.println("1.數組複製:");
        copy();
        int a[] = {1, 2, 3, 12};//聲明數組並初始化
        int b[] = {5, 6, 7, 8};
        System.out.println("2.數組合並:");
        int c[] = combine(a, b);//調用數組合並方法
        System.out.println("3.數組插入:");
        insert();//調用數組插入方法
    }
}
/*
(1)在copy()方法中,運用System。arraycopy()方法對數組進行復制。方法中聲明兩個數組,其中array數組在聲明時初始化。
temp1數組的長度爲array數組的長度以便進行復制。進行復制時arraycopy(array,0,temp1,1,array.length)方法
表示將array數組從第一個數組從第一個元素開始複製到temp1數組下標從0到array.length的位置。
(2)在insert()方法中,運用循環對數組初始化並排好序。接受通過鍵盤輸入的待插入的數值。循環線查找第一個大於要插入數組的位置
,然後在運用循環爲要插入的數組留出位置,將待插入的數放到該位置。
(3)在combine()方法中,聲明一個新數組,長度爲傳入數組的長度之和。循環查看傳入的兩個數組元素,
將兩個數組的元素依次進行大小比較,比較之後的元素放入新數組。通過arraycopy()方法將末放入新數組的元素複製進去,完成數組的合併與新數組的排序。
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章