【问题标题】:return string of odd elements in a linked list返回链表中奇数元素的字符串
【发布时间】:2013-12-01 17:19:22
【问题描述】:

我需要编写一个方法——返回一个字符串,其中奇数元素的 toStrings 之间用冒号连接。换句话说,第 1、第 3、第 5、... 元素。

我已经编写了一个方法来返回所有元素的字符串:

public String toString (Node<E> node) {
    if (node == null) {
        return "";
    }
    else {
        Node<E> next = node.next ();
        return node.element () + (next == null ? "" : "\n") + toString (next);
    }
}
//Return String with all elements
public String toString() {
    return toString(head);
}

此代码未经测试..

这是我第一次使用链表,所以我不确定是否有一个简单的单线来完成这个或者我需要使用什么逻辑?

感谢所有帮助。

【问题讨论】:

  • 我喜欢你在这里使用的递归方法。但对于其他所有元素,查看for 循环可能更容易。
  • 为什么没有测试?
  • @peeskillet - 我认为调用 具有非 void 参数 toString 的方法是一个不幸的选择; oddElementsAsString 似乎是一个更好的名字(它应该是静态的,因为它接收一个参数)
  • @jonhopkins 是的,这就是我所倾向于的,但我的问题确实出在逻辑上,链表是否有办法抓取所有其他元素或者如何做到这一点?
  • 现在想想,我不知道你的Node 类是否有length 属性,所以while 循环可能会更好。但我会用一个计数器来做,它会增加循环的每次迭代,以及添加到字符串中的每个其他循环。

标签: java linked-list tostring


【解决方案1】:

您应该稍微更改一下代码才能正常工作:

public String toString (Node<E> node) {
    if (node == null) {
        return "";
    }
    else {
        Node<E> next = node.next ();
        if(next == null){
           return node.element().toString(); //avoid null pointer exceptions
        }

        return node.element () + "\n" + toString(next.next());//continue recursion with an odd element
    }
}

此代码可以改进。

【讨论】:

    【解决方案2】:

    为了获取链表中的所有其他元素,我会使用带有计数器的while 循环来跟踪我们所在的元素,因此只有正确的元素会添加到字符串中。由于偶数可以很容易地用number % 2 == 0 表示,奇数用number % 2 == 1 表示,只要计数器是最新的,就可以轻松判断当前节点是偶数还是奇数。

    在此示例中,循环必须遍历列表中的每个节点,并且仅当计数器为偶数时才添加到字符串中(或者如果您想将其更改为奇数)。

    public String toString(Node<E> node) {
        if (node == null) {
            return "";
        }
    
        String elements = ""; // I would recommend looking into StringBuilder
        int counter = 0;
    
        // add the first element to the string right away.
        // this will eliminate accidentally putting a \n at the beginning
        // of the string, before any elements have been added to it
        elements = node.element; // if you want odd elements instead of 
        node = node.next();      // even, swap these two lines.
        counter++;
    
        while (node != null) {
            if (counter % 2 == 0) { // only add the even elements
                                    // this can easily be changed for odd elements
                elements = elements + "\n" + node.element;
            }
            node = node.next(); // go to the next node every iteration of the loop
            counter++; // always increment the counter
        }
    
        return elements;
    }
    

    【讨论】:

    • 好的,谢谢,我把它弄复杂了,一如既往。我确实有一个问题,关于 elements = node.element;我收到类型不匹配错误,我使用强制转换元素 = (String) node.element... 修复了它,但这是正确的修复方法还是那里有其他问题?
    • 这取决于node.element 实际上是什么。如果它有效,那么很可能这是正确的解决方案。如果element 是某种对象,我只会担心转换为String。
    猜你喜欢
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多