Java 返回链表中奇数元素的字符串

Java 返回链表中奇数元素的字符串,java,linked-list,tostring,Java,Linked List,Tostring,我需要编写一个方法–返回一个字符串,其中奇数元素的toString之间用冒号连接。换句话说,第一、第三、第五……元素 我已经编写了一个方法来返回所有元素的字符串: public String toString (Node<E> node) { if (node == null) { return ""; } else { Node<E> next = node.next (); return node

我需要编写一个方法–返回一个字符串,其中奇数元素的toString之间用冒号连接。换句话说,第一、第三、第五……元素

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

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);
}
公共字符串到字符串(节点){
if(node==null){
返回“”;
}
否则{
Node next=Node.next();
返回node.element()+(next==null?”:“\n”)+toString(next);
}
}
//返回包含所有元素的字符串
公共字符串toString(){
返回到管柱(头部);
}
此代码尚未经过测试

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


感谢您提供的所有帮助。

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

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
    }
}
公共字符串到字符串(节点){
if(node==null){
返回“”;
}
否则{
Node next=Node.next();
if(next==null){
return node.element().toString();//避免空指针异常
}
return node.element()+“\n”+toString(next.next());//使用奇数元素继续递归
}
}

这段代码可以改进

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

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

公共字符串到字符串(节点){
if(node==null){
返回“”;
}
String elements=“”;//我建议查看StringBuilder
int计数器=0;
//立即将第一个元素添加到字符串中。
//这将避免在开始时意外放置\n
//在向字符串中添加任何元素之前
elements=node.element;//如果需要奇数元素而不是
node=node.next();//偶数,交换这两行。
计数器++;
while(节点!=null){
如果(计数器%2==0){//只添加偶数元素
//对于奇数元素,可以很容易地进行更改
元素=元素+“\n”+节点元素;
}
node=node.next();//在循环的每次迭代中转到下一个节点
计数器+++;//始终递增计数器
}
返回元素;
}

我喜欢您在这里使用的递归方法。但是对于其他所有元素,可能更容易查看
循环的
。为什么不测试它?@peeskillet-我认为调用带有非无效参数的方法来调用字符串是一个不幸的选择
oddElementsAsString
似乎是一个更好的名称(它应该是静态的,因为它收到了一个参数)@jonhopkins是的,这就是我倾向于的,但我的问题真正在于逻辑,链表是否有办法抓取所有其他元素,或者如何做到这一点?现在我想起来了,我不知道您的
节点
类是否具有
长度
属性,因此
while
循环可能更好。但是我会用一个计数器来增加循环的每次迭代,以及你添加到字符串中的每个循环。我有一个问题,关于elements=node.element;我得到了一个类型不匹配错误,我用casting元素=(String)node.element修复了它。。。但这是正确的修复方法还是有其他问题?这取决于
node.element
实际上是什么。如果它起作用,很可能就是正确的解决方案。如果
元素
是某种类型的对象,我只关心转换为
字符串
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;
}