如何在Java中反转一系列连接的对象。

huangapple go评论58阅读模式
英文:

How do I reverse a sequence of attached objects in java

问题

/**
 * 递归地反转从此 Wagon 开始直至其最终后继的序列顺序。
 * 反转后的序列再次连接到此 Wagon 的前身,如果有的话。
 * 如果此 Wagon 没有后继的下一节车厢连接,则不执行任何操作。
 * @return 反转序列的新起始 Wagon(原始序列的最后一个 Wagon)
 */
public Wagon reverseSequence() {
    if (!hasNextWagon()) {
        return this;
    } else {
        Wagon restReversed = getNextWagon().reverseSequence();
        getNextWagon().setNextWagon(this);
        setNextWagon(null);
        return restReversed;
    }
}

第一个测试代码:

@Test
public void T05_WholeSequenceOfFourShouldBeReversed() {
    passengerWagon2.attachTo(passengerWagon1);
    passengerWagon3.attachTo(passengerWagon2);
    passengerWagon4.attachTo(passengerWagon3);

    // 反转完整序列
    Wagon rev = passengerWagon1.reverseSequence();

    assertEquals(4, rev.getSequenceLength());
    assertEquals(passengerWagon4, rev);
    assertEquals(passengerWagon3, rev.getNextWagon());
    assertFalse(rev.hasPreviousWagon());

    assertEquals(passengerWagon2, passengerWagon3.getNextWagon());
    assertEquals(passengerWagon4, passengerWagon3.getPreviousWagon());

    assertEquals(passengerWagon1, passengerWagon2.getNextWagon());
    assertEquals(passengerWagon3, passengerWagon2.getPreviousWagon());

    assertFalse(passengerWagon1.hasNextWagon());
    assertEquals(passengerWagon2, passengerWagon1.getPreviousWagon());
}

第二个测试代码:

@Test
public void T05_PartiallyReverseASequenceOfFour() {
    passengerWagon2.attachTo(passengerWagon1);
    passengerWagon3.attachTo(passengerWagon2);
    passengerWagon4.attachTo(passengerWagon3);

    // 反转部分序列
    Wagon rev = passengerWagon3.reverseSequence();
    assertEquals(2, rev.getSequenceLength());
    assertEquals(passengerWagon4, rev);

    assertEquals(passengerWagon3, rev.getNextWagon());
    assertEquals(passengerWagon2, rev.getPreviousWagon());

    assertFalse(passengerWagon3.hasNextWagon());
    assertEquals(passengerWagon4, passengerWagon3.getPreviousWagon());

    assertEquals(4, passengerWagon1.getSequenceLength());
    assertFalse(passengerWagon1.hasPreviousWagon());
    assertEquals(passengerWagon2, passengerWagon1.getNextWagon());

    assertEquals(passengerWagon1, passengerWagon2.getPreviousWagon());
    assertEquals(passengerWagon4, passengerWagon2.getNextWagon());
}
英文:

My Wagon object has 2 variables, Nextwagon and Previouswagon. These wagons can be attached to each other using next and previous wagon variables. For example if I have 4 wagons, wagon 2 is attached to the first by declaring the nextwagon of the first as wagon 2 and the third is attached to the second and so forth.

As a result my sequence would be 1,2,3,4. My question is; I want to reverse this sequence with the help of a recursive method (which should result as 4,3,2,1.), how can I achieve this?

 /**
 * reverses the order in the sequence of wagons from this Wagon until its final successor.
 * The reversed sequence is attached again to the predecessor of this Wagon, if any.
 * no action if this Wagon has no succeeding next wagon attached.
 * @return the new start Wagon of the reversed sequence (with is the former last Wagon of the original sequence)
 */
public Wagon reverseSequence() {
    // TODO provide a recursive implementation


    return null;
}

The following code snipped is a test that should pass if my method is correct.

@Test
public void T05_WholeSequenceOfFourShouldBeReversed() {
    passengerWagon2.attachTo(passengerWagon1);
    passengerWagon3.attachTo(passengerWagon2);
    passengerWagon4.attachTo(passengerWagon3);

    // reverse full sequence
    Wagon rev = passengerWagon1.reverseSequence();

    assertEquals(4, rev.getSequenceLength());
    assertEquals(passengerWagon4, rev);
    assertEquals(passengerWagon3, rev.getNextWagon());
    assertFalse(rev.hasPreviousWagon());

    assertEquals(passengerWagon2, passengerWagon3.getNextWagon());
    assertEquals(passengerWagon4, passengerWagon3.getPreviousWagon());

    assertEquals(passengerWagon1, passengerWagon2.getNextWagon());
    assertEquals(passengerWagon3, passengerWagon2.getPreviousWagon());

    assertFalse(passengerWagon1.hasNextWagon());
    assertEquals(passengerWagon2, passengerWagon1.getPreviousWagon());
}

The following test should also pass.

@Test
public void T05_PartiallyReverseASequenceOfFour() {
    passengerWagon2.attachTo(passengerWagon1);
    passengerWagon3.attachTo(passengerWagon2);
    passengerWagon4.attachTo(passengerWagon3);

    // reverse part of the sequence
    Wagon rev = passengerWagon3.reverseSequence();
    assertEquals(2, rev.getSequenceLength());
    assertEquals(passengerWagon4, rev);

    assertEquals(passengerWagon3, rev.getNextWagon());
    assertEquals(passengerWagon2, rev.getPreviousWagon());

    assertFalse(passengerWagon3.hasNextWagon());
    assertEquals(passengerWagon4, passengerWagon3.getPreviousWagon());

    assertEquals(4, passengerWagon1.getSequenceLength());
    assertFalse(passengerWagon1.hasPreviousWagon());
    assertEquals(passengerWagon2, passengerWagon1.getNextWagon());

    assertEquals(passengerWagon1, passengerWagon2.getPreviousWagon());
    assertEquals(passengerWagon4, passengerWagon2.getNextWagon());
}

答案1

得分: 2

我可能会迟到,但我为您提供了答案。但是,我不被允许用代码回答这个问题。因为这个作业是为了分数,将会在明年使用,是的,我来自同一所学校 如何在Java中反转一系列连接的对象。

解决方案:
每次递归,您必须从中获取最后一个车厢(getLastWagonAttached()),并将该车厢与其前车分离(detachFromPrevious())。如果最后一个车厢与当前车厢相同,则必须返回其自身。

在分离当前递归的最后一个车厢之后,将其附加到所调用递归的最后一个车厢(注意:每次递归都在同一辆车厢上)。然后返回当前递归的最后一个车厢。

现在,您已经颠倒了顺序。但它与列车的未颠倒部分没有连接(注意:如果车厢有先前的序列)。因此,因为您在同一辆车厢上调用了递归,您可以将其重新附加到其原始先前车厢。

英文:

I might be late, but I have the answer for you. However, I am not allowed to answer this with code. Because this assignment is for a grade and will be used next year and yes I'm from the same school. 如何在Java中反转一系列连接的对象。

The solution:
Every recursion you have to get the last wagon (getLastWagonAttached()) from and detach this wagon from its predecessors (detachFromPrevious()). If the last wagon is the same as the current wagon you have to return itself.

After detaching the current recursion's last wagon, attach it to the called recursion's last wagon (Note: Every recursion happens on the same wagon) And then return the current recursion's last wagon.

Now you've reversed the sequence. But is not connected to the not reversed part of the train. (Note: If the wagon had a previous sequence) So because you called the recursion on the same wagon you can attach it back to its original previous wagon.

答案2

得分: 0

public Wagon reverseSequence() {
    Wagon nextWagon = this.getNextWagon();
    if (nextWagon == null) {
        return this;
    }
    Wagon front = nextWagon.reverseSequence();
    this.attachTo(nextWagon);
    return front;
}

在这里可以看到两个操作:首先,我们驱动到火车车厢的末尾,挑出最后一个车厢。然后,我们将它拖到最前面,每个车厢都会连接到之前连接到它的车厢。这样一来,火车车厢就被倒转了。

(请注意,我不知道 .getNextWagon().attachTo() 实际上执行什么操作,因为我无法访问您的源代码 - 我正在做出合理的猜测,如果我猜错了,希望您能察觉并为您自己的解决方案进行更正)

英文:
public Wagon reverseSequence() {
    // first, get the next wagon in the sequence
    Wagon nextWagon = this.getNextWagon();
    // if it is the last wagon in the train, then it should be the first wagon
    // in the new sequence. Return it. This is the recursive base case.
    if (nextWagon == null) {
        return this;
    }
    // otherwise, do three things:
    // (1) reverse the part of the wagon train after this
    // (2) attach this wagon to _after_ what was previously the next wagon,
    //       since after being reversed it will be at the end of the train
    //       before this wagon
    // (3) return the front of that wagon train
    Wagon front = nextWagon.reverseSequence();
    this.attachTo(nextWagon);
    return front;
}

You can see two things happening here: first, we drive to the end of the wagon train and pick out the last wagon. Then, we drag it to the front, and each wagon cart attaches itself to the cart that was previously attached to it. And voila, the wagon train is reversed.

(note that I don't know what .getNextWagon() and .attachTo() actually do, since I don't have access to your source code - I'm making an educated guess, so hopefully if I'm wrong you can realize and correct it for your own solution)

huangapple
  • 本文由 发表于 2020年9月18日 03:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定