package com.ccc.chess;
/**
*
* @author Carol
*
*/
public class ArrayComp {
public int[] Combine(int[] a,int[] b){
int temp=0;
int temp_b=0;
int temp_a=0;
int aLenght=0;
int bLenght=0;
int cLength=0;
if(a==null&&b==null){
return null;
}
if(a!=null){
aLenght=a.length;
}
if(b!=null){
bLenght=b.length;
}
cLength=aLenght+bLenght;
int[] c = new int[cLength];
if(a.length==0){
for(int i=0;i<b.length;i++){
c[temp++]=b[i];
}
return c;
}
else if(b.length==0){
for(int i=0;i<a.length;i++){
c[temp++]=a[i];
}
return c;
}
for(int i=temp_a;i<a.length;i++){
for(int j=temp_b;j<b.length;j++){
System.out.println("==temp_b is"+temp_b+" a["+i+"] is"+a[i]+" b["+j+"] is"+b[j]);
if(a[i]<=b[j]){
c[temp]=a[i];
System.out.println("找到A中的數值小于B中數值﹐將跳出B循環 。開始下個A的比較!! c["+temp+"] is "+c[temp]);
temp++;
temp_b=j;
temp_a=i+1;
if((i==a.length-1)&&j<b.length){//如果A循環完畢可是B還沒有循環完,就需要把B后面的元素接上
for(int m=j;m<b.length;m++){
c[temp++]=b[m];
}
}
break;
}
else{
temp_b=j+1;
c[temp]=b[j];
System.out.println("繼續循環在B中循環!! c["+temp+"] is "+c[temp]);
temp++;
if(j==b.length-1&&i<a.length){
for(int m=i;m<a.length;m++){//如果B循環完畢可是A還沒有循環完,就需要把A后面的元素接上
c[temp++]=a[m];
}
}
}
}
}
return c;
}
public static void main(String[] args) {
ArrayComp ArrayComp = new ArrayComp();
int[] a = new int[]{1,3,5,7,11};
int[] b = new int[]{2,4,6,8,10};
int [] c =ArrayComp.Combine(a, b);
for(int i=0;i<c.length;i++){
System.out.println("c["+i+"] 是"+c[i]);
}
}
}
/**
*
* @author Carol
*
*/
public class ArrayComp {
public int[] Combine(int[] a,int[] b){
int temp=0;
int temp_b=0;
int temp_a=0;
int aLenght=0;
int bLenght=0;
int cLength=0;
if(a==null&&b==null){
return null;
}
if(a!=null){
aLenght=a.length;
}
if(b!=null){
bLenght=b.length;
}
cLength=aLenght+bLenght;
int[] c = new int[cLength];
if(a.length==0){
for(int i=0;i<b.length;i++){
c[temp++]=b[i];
}
return c;
}
else if(b.length==0){
for(int i=0;i<a.length;i++){
c[temp++]=a[i];
}
return c;
}
for(int i=temp_a;i<a.length;i++){
for(int j=temp_b;j<b.length;j++){
System.out.println("==temp_b is"+temp_b+" a["+i+"] is"+a[i]+" b["+j+"] is"+b[j]);
if(a[i]<=b[j]){
c[temp]=a[i];
System.out.println("找到A中的數值小于B中數值﹐將跳出B循環 。開始下個A的比較!! c["+temp+"] is "+c[temp]);
temp++;
temp_b=j;
temp_a=i+1;
if((i==a.length-1)&&j<b.length){//如果A循環完畢可是B還沒有循環完,就需要把B后面的元素接上
for(int m=j;m<b.length;m++){
c[temp++]=b[m];
}
}
break;
}
else{
temp_b=j+1;
c[temp]=b[j];
System.out.println("繼續循環在B中循環!! c["+temp+"] is "+c[temp]);
temp++;
if(j==b.length-1&&i<a.length){
for(int m=i;m<a.length;m++){//如果B循環完畢可是A還沒有循環完,就需要把A后面的元素接上
c[temp++]=a[m];
}
}
}
}
}
return c;
}
public static void main(String[] args) {
ArrayComp ArrayComp = new ArrayComp();
int[] a = new int[]{1,3,5,7,11};
int[] b = new int[]{2,4,6,8,10};
int [] c =ArrayComp.Combine(a, b);
for(int i=0;i<c.length;i++){
System.out.println("c["+i+"] 是"+c[i]);
}
}
}