๋ฐ์ํ
๋ชฉํ
๋๊ฐ์ ๋ฐฐ์ด A์B๋ฅผ ํฉ๋ณ ํ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ๋ฐํํ๋ mergeArray๋ฉ์๋๋ฅผ ์์ฑํ์์ค.
์ฝ๋
package JavaFestival;
import java.util.Arrays;
public class test4 {
public static void main(String[] args) {
int[] A = { 1, 3, 5, 7, 9};
int[] B = { 2, 4, 5, 8, 10};
int[] C = new int[A.length + B.length];
int[] Merge = mergeArray(A, B);
System.out.print("Merge : "+Arrays.toString(Merge));
}
public static int[] mergeArray(int[] a, int[] b) {
int pa = 0;
int pb = 0;
int pc = 0;
int na = a.length;
int nb = b.length;
int[] c = new int[a.length + b.length];
while (pa < na && pb < nb) {
c[pc++] = ( a[pa]>= b[pb] ) ? b[pb++]: a[pa++];
// Merge : [1, 2, 3, 4, 5, 5, 7, 8, 9, 0]
}
// 2. ๋งจ ๋ง์ง๋ง ํ, ์ง์ ๊ฒฝ์ฐ 0์ด ์๋ ๋ง์ง๋ง ์ซ์ ๋ฐํ
while(pa < na) {
c[pc++] = a[pa++];
}
while(pb < nb) {
c[pc++] = b[pb++];
}
return c;
}
}
๊ฒฐ๊ณผ
Merge : [1, 2, 3, 4, 5, 5, 7, 8, 9, 10]
๋ฐ์ํ
'๐๏ธ์ํํธ์จ์ด > ๐java & Servlete & JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Servlet] ์๋ธ๋ ์ด๋? (0) | 2021.09.14 |
---|---|
JSP (0) | 2021.09.13 |
[์๋ฐ/Java] 1์ฐจ ์ํ ์ค๋น (1) (0) | 2021.09.05 |
[์๋ฐ/Java] ์ธํฐํ์ด์ค(interface) (0) | 2021.09.05 |
[์๋ฐ/Java] 1๋ณด๋ค ํฐ ์ ์๋ฅผ ์ ๋ ฅํ์ฌ ํฉํ ๋ฆฌ์ผ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ (0) | 2021.09.05 |