【问题标题】:How do I merge two objects in Java?如何在 Java 中合并两个对象?
【发布时间】:2019-10-08 09:47:56
【问题描述】:

对于我的家庭作业,我需要在 Java 中合并两个对象。我必须创建一个方法 void mergeTrains(Train other) 这会将来自另一列火车的所有机车和汽车添加到当前火车。因此,在调用train1.mergeTrains(train2) 之后,参数 train 将在完成后没有汽车和机车。它们都将被添加到 train1。

然后我们必须使用 JUnit 来检查数组和机车是否正确合并。 这是我目前所拥有的:

public class Train {

    // data members
    private String trainName;
    private int numOfLocomotives;
    private int[] freightCars;

    // constructor
    public Train (String trainName, int numOfLocomotives) {
        this.trainName = trainName;
        this.numOfLocomotives = numOfLocomotives;
    }

    public int getNumOfLocomotives() {
        return numOfLocomotives;
    }

    public void setNumOfLocomotives(int numOfLocomotives) {
        if (numOfLocomotives < 0) {
            System.out.println("Error. Locomotives can't be set to less than 0.");
            return;
        } else {
            this.numOfLocomotives = numOfLocomotives;
        }
    }

    public int[] getFreightCars(int... freightCars) {
        if (freightCars == null) {
            System.out.println("There are no freight cars in the list.");
            return freightCars;
        } else {
            return freightCars;
        }
    }

    public int[] removeAllCars(int...freightCars) {
        freightCars = null;
        return freightCars;
    }

    public int[] addCars(int...weights) {
        int count = 0;
        int[] freightCars = {10, 20, 30}; // used to check if method functions correctly
        int[] newTrain = new int[freightCars.length + weights.length];
        for (int i = 0; i < freightCars.length; i++) {
            newTrain[i] = freightCars[i];
            count++;
        }
        for (int j = 0; j < weights.length; j++) {
            newTrain[count++] = weights[j];
        }
        for (int i = 0; i < newTrain.length; i++) {
            System.out.print(newTrain[i] + " ");
        }
        return newTrain;
    }

    public void mergeTrains(Train other) {
        this.numOfLocomotives = this.numOfLocomotives + other.numOfLocomotives;
        other.numOfLocomotives = 0;

    }

这是我的 JUnit 测试类:

class TestTrain {

    int[] train1Cars = {10, 20, 30};
    int[] train2Cars = {4, 11, 15};
    int[] mergedTrain = {10, 20, 30, 4, 11, 15};

    @Test
    void testRemoveCars() {
        Train t1 = new Train("Thomas", 2);
        assertArrayEquals(t1.removeAllCars(train1Cars), null);
    }

    @Test
    void testAddCars() {
        Train t1 = new Train ("Thomas", 2);
        assertArrayEquals(t1.addCars(train2Cars), mergedTrain);
    }

    @Test
    void testMergeTrains() {
        Train t1 = new Train ("Thomas", 1);
        Train other = new Train ("Rob", 4);
    }
}

基本上我需要弄清楚我应该如何处理Train other 参数。然后我如何使用 JUnit 进行测试。我认为在 mergeTrains 方法中更改机车是正确的,但我也不知道如何在 JUnit 中进行检查。

【问题讨论】:

    标签: java arrays object merge


    【解决方案1】:

    我认为,当您将一列火车合并到另一列火车时,您会将 train2 中的机车添加到 train1,然后将 train2 中的数组中的货车添加到 train1 中的数组中。后一步将涉及复制数组。我建议您查看 Java 方法 System.arraycopy() 来执行此操作。

    要执行这两个操作,您必须使用传递给合并方法的 Train 实例。

    我还希望 train2 中的机车和发动机清零。可以使用一个新的空数组来替换旧数组。但这取决于您的讲师希望您如何表明这一点。

    【讨论】:

      【解决方案2】:

      数组中的值是一个实现细节。让我们来看看每列火车的状态。

      例如:

          @Test
          void can_add_a_car() {
              Train t1 = new Train("Thomas", 2);
              t1.addCars(1, 1, 1);
              assertEquals(t1.numberOfCars(), 3);
          }
      

      所以这个类开始看起来像

      public class Train {
          private ...instance variables ...
      
          public addCars(int... weights) { ... }
      }
      

      要是能拆车就好了

      @Test
      void can_remove_a_car() {
          Train t1 = new Train("Thomas", 2);
          t1.addCars(1, 2, 3);
          t1.removeCar(2);
          assertEquals(t1.numberOfCars(), 2);
          assertEquals(t1.weight(), 3);
      }
      

      这可能会建议像

      这样的方法
      public int removeCar(int position) {
          // TODO: move all the cars up by one
          return freightCars[position];
      }
      
      public int weight() {
          // sum the weights of the cars
      }
      

      合并汽车如下所示:

      @Test
      void can_merge_trains() {
          Train t1 = new Train("Thomas", 2);
          Train t2 = new Train("Rob", 2);
          t1.addCars(1, 1, 1)
          t2.addCars(1, 1, 1)
          t1.merge(t2);
          assertEquals(t1.numberOfCars(), 6);
          assertEquals(t2.numberOfCars(), 0);
      }
      

      我们可以这样实现这个方法:

      public void mergeCars(Train other) {
          for(int k = other.numberOfCars() - 1; k >= 0; k--) {
              addCar(other.removeCar());
          }
      }
      

      您也可以添加方法来添加和删除机车,并在 mergeCars 中使用这些方法。

      可能有更有效的方法来实现数组复制,而不是一次只做一辆车。如果性能出现问题,您应该能够添加批量添加和删除方法。

      【讨论】:

      • 当我测试assertEquals(t1.numberOfCars(), 6); 时,它认为因为括号是空的,所以该值为空。除非我专门输入这样的 6:assertEquals(t1.numberOfCars(6), 6);
      • 您的 numberOfCars 方法签名应该是 int numberOfCars(),您还定义了其他东西,它采用可变数量的 int 并返回这些值。该方法不应接受任何参数并返回单个整数。
      猜你喜欢
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      相关资源
      最近更新 更多