Java 具有复数元素的两个数组的并集

Java 具有复数元素的两个数组的并集,java,Java,我要做一个程序,可以把两个数组和复数元素结合起来。我定义了复数类,将复数分配给数组。问题是我写了一个可以合并这两个数组的方法。我知道使用集合可以很容易地解决这个问题,但我只需要使用数组来解决这个问题,这是针对我的it类的。我将把我的代码放在下面,注释行是我需要实现代码的地方。我和这个问题纠缠了很长时间。请帮帮我 import java.util.Arrays; /** * * @author Dragosh */ public class Multime { //Here i assig

我要做一个程序,可以把两个数组和复数元素结合起来。我定义了复数类,将复数分配给数组。问题是我写了一个可以合并这两个数组的方法。我知道使用集合可以很容易地解决这个问题,但我只需要使用数组来解决这个问题,这是针对我的it类的。我将把我的代码放在下面,注释行是我需要实现代码的地方。我和这个问题纠缠了很长时间。请帮帮我

import java.util.Arrays;

/**
 *
 * @author Dragosh
 */
public class Multime {
//Here i assign values for complex numbers and asign them to arrays A and B
static Complex a1 = new Complex (4.0, 6.0);
static Complex a2 = new Complex (-2.0, 4.0);
static Complex a3 = new Complex (5.0, 7.0);
static Complex [] A = {a1,a2,a3}; 
static Complex b1 = new Complex (3.0, 6.0);
static Complex b2 = new Complex (-2.0, 4.0);
static Complex b3 = new Complex (-7.0, 5.0);
static Complex [] B = {b1,b2,b3};


//Here i define my union method with A and B arrays as arguments
public static Complex[] reuniune(Complex[] A, Complex[] B){
 int marime = A.length+B.length;//This is the length of the C array, A+B
 Complex[] C = new Complex[marime];//Here i assign the length to C array
 int i;
 int j;
 for(i=0;i<A.length;i++){//Here i go through my A array
 C[i]=A[i];//Here i assign A values to C values
 }
 for (j=0;j<B.length;j++){//Here i go through my B array
 //HERE I NEED TO WRITE THE CODE TO ASSIGN THE B VALUES TO C, WHICH ARE NOT IN A ARRAY
  }
 return C;
}

    public static void main(String[] args) {
    System.out.println(Arrays.toString(reuniune(A,B)));

    }

public static class Complex{ // Here is the definition of Complex numbers class
    private final double re;
    private final double im;

    public Complex(double real, double imag){
        re=real;
        im=imag;
    }

    public String toString(){ // Here is the converting the Complex class to string  
//like Real+Imaginary*i        
        if (im==0) return re + "";
        if (re==0) return im + "i";
        if (im<0) return re + "-" + (-im) + "i";
        return re + " + " + im + "i";
    }


}

}
导入java.util.array;
/**
*
*@作者德拉戈什
*/
公共类多址{
//这里我为复数赋值,并将它们赋给数组A和B
静态复合体a1=新复合体(4.0,6.0);
静态复合物a2=新复合物(-2.0,4.0);
静态复合体a3=新复合体(5.0,7.0);
静态络合物[]A={a1,a2,a3};
静态复合体b1=新复合体(3.0,6.0);
静态复合体b2=新复合体(-2.0,4.0);
静态复合体b3=新复合体(-7.0,5.0);
静态络合物[]B={b1,b2,b3};
//这里我用A和B数组作为参数定义union方法
公共静态综合体[]reuniune(综合体[]A、综合体[]B){
int marime=A.length+B.length;//这是C数组的长度,A+B
Complex[]C=newcomplex[marime];//这里我将长度分配给C数组
int i;
int j;

对于(i=0;i我可能会这样做,检查B中的每个元素是否在A中:

for (j=0;j<B.length;j++){//Here i go through my B array
   boolean found = false;

   for (int k = 0; k < A.length; k++) {    // check if element from B was already in A
      if (A[k].equals(B[j])) {       
          found = true;
          break;
      }
   }

   if (!found) {           // if B[j] was not in A, add it to C
      C[i] = B[j];
      i++;
   }
}

pbm是指C索引不匹配。以下是解决方案:

public static Complex[] reuniune(Complex[] A, Complex[] B) {
        int marime = A.length + B.length;// This is the length of the C array,
                                            // A+B
        Complex[] C = new Complex[marime];// Here i assign the length to C array
        int i;
        int j;
        int k=0;
        for (i = 0; i < A.length; i++) {// Here i go through my A array
            C[k] = A[i];// Here i assign A values to C values
            k++;
        }
        for (j = 0; j < B.length; j++) {// Here i go through my B array
            // HERE I NEED TO WRITE THE CODE TO ASSIGN THE B VALUES TO C, WHICH
            // ARE NOT IN A ARRAY
            C[k] = B[j];
            k++;
        }
        return C;
    }
public static Complex[]reuniune(Complex[]A,Complex[]B){
int marime=A.length+B.length;//这是C数组的长度,
//A+B
Complex[]C=newcomplex[marime];//这里我将长度分配给C数组
int i;
int j;
int k=0;
对于(i=0;i
这不好,因为我们从一个数组中丢失了两个元素,(4.0,6.0)和(5.0,7.0)。您需要为您的
复杂的
类实现
equals()
。我测试了它,它与我发布的代码一起工作。只需实现
equals
是的,但值(-2.0,4.0i)在C数组中加两次,而不是一次,因为你没有等式:P,是吗^^
public static Complex[] reuniune(Complex[] A, Complex[] B) {
        int marime = A.length + B.length;// This is the length of the C array,
                                            // A+B
        Complex[] C = new Complex[marime];// Here i assign the length to C array
        int i;
        int j;
        int k=0;
        for (i = 0; i < A.length; i++) {// Here i go through my A array
            C[k] = A[i];// Here i assign A values to C values
            k++;
        }
        for (j = 0; j < B.length; j++) {// Here i go through my B array
            // HERE I NEED TO WRITE THE CODE TO ASSIGN THE B VALUES TO C, WHICH
            // ARE NOT IN A ARRAY
            C[k] = B[j];
            k++;
        }
        return C;
    }