如下所示:
package com.test.sort;public class testMerge {public static void main(String[] args) {int[] a = { 1, 3, 5 };int[] b = { 2, 3, 4, 7 };merge m = new merge();m.method(a, b);}}class merge {public void method(int[] a, int[] b) {int l = a.length + b.length;int[] temp = new int[l];int i = 0, j = 0, h = 0;// 這里必須用while,不能用ifwhile (i < a.length || j < b.length) {if (i == a.length && j < b.length) {temp[h++] = b[j++];} else if (i < a.length && j == b.length) {temp[h++] = a[i++];} else if (a[i] <= b[j]) {temp[h++] = a[i++];} else if (a[i] > b[j]) {temp[h++] = b[j++];}}for (int m : temp) {System.out.print(m + " ");}}}
以上這篇java實現把兩個有序數組合并到一個數組的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選