简体   繁体   English

有人能告诉我我做错了什么吗? 通过LinkedList计数和循环

[英]Can someone tell me what i'm doing wrong? Counting and looping through LinkedList

My code is as follows: 我的代码如下:

import net.datastructures.Node;

public class SLinkedListExtended<E> extends SLinkedList<E> {

public int count(E elem) {
    Node <E> currentNode = new Node <E>();
    currentNode = head;
    int counter = 0;

    for (int i = 0; i<size; i++){

    if (currentNode == null) {
        return 0; //current is null
    }
    else if (elem.equals(currentNode.getElement())){
                counter++;
                currentNode = currentNode.getNext();
            }
    }
    return counter;
    }



public static void main(String[] args) {

    SLinkedListExtended<String> x = new SLinkedListExtended<String>();

    x.insertAtTail("abc");
    x.insertAtTail("def");
    x.insertAtTail("def");
    x.insertAtTail("xyz");
    System.out.println(x.count("def")); // should print "2"
    //x.insertAtTail(null);
    x.insertAtTail("def");
    //x.insertAtTail(null);
    System.out.println(x.count("def")); // should print "3"
    //System.out.println(x.count(null)); // should print "2"
}

}

The method count is supposed to return the number of the amount of times a given element, elem is found in a list. 方法计数应该返回给定元素elem在列表中找到的次数。 I have written this loop but only get a return of 0 every time. 我已经编写了这个循环,但每次只返回0。 A nullpointerexception is also thrown. 还会抛出nullpointerexception。

Edit: SLinkedList SuperClass 编辑:SLinkedList SuperClass

import net.datastructures.Node;

public class SLinkedList<E> {
protected Node<E> head; // head node of the list
protected Node<E> tail; // tail node of the list (if needed)
protected long size; // number of nodes in the list (if needed)

// default constructor that creates an empty list
public SLinkedList() {
    head = null;
    tail = null;
    size = 0;
}

// update and search methods
public void insertAtHead(E element) {
    head = new Node<E>(element, head);
    size++;
    if (size == 1) {
        tail = head;
    }
}

public void insertAtTail(E element) {
    Node<E> newNode = new Node<E>(element, null);
    if (head != null) {
        tail.setNext(newNode);
    } else {
        head = newNode;
    }
    tail = newNode;
    size++;
}



public static void main(String[] args) { // test


}
}

It seems you missed to go to the next node if non of both condition match. 如果两个条件都不匹配,你似乎错过了去下一个节点。

public int count(E elem) {
    Node <E> currentNode = new Node <E>();
    currentNode = head;
    int counter = 0;

    for (int i = 0; i<size; i++){
        if (currentNode == null) {
            return 0; //current is null
        }
        else if (elem.equals(currentNode.getElement())){
            counter++;
        }
        currentNode = currentNode.getNext();          
    }
    return counter;
}

MrSmith's answer nails it, I think. 我想,史密斯先生的回答是钉在它上面的。 I would not use size for the loop but take the fact that there is no next as bottom. 我不会使用大小的循环,但采取的事实是没有下一个作为底部。 Of course your count method then has to return the counter in all cases and not 0. 当然,你的计数方法必须在所有情况下返回计数器,而不是0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我在Java中设置此Robot类时我做错了什么吗? - Can someone tell me what I'm doing wrong to setup this Robot class in Java? 谁能告诉我我在做什么错? -堆栈 - Can anyone tell me what I'm doing wrong? - Stacks HackerRank 任务“Mini Max Sum”解决方案未通过 13 个测试用例中的 3 个,有人能告诉我我做错了什么吗 - HackerRank Task “Mini Max Sum” solution not passing 3 of the 13 test cases, can someone tell me what i'm doing wrong 有一种有效的“合并排序”功能,但不是完全可以,有人可以告诉我我做错了什么吗? - Have a sort of working Merge Sort, but not quite, could someone tell me what I am doing wrong? 请告诉我我做错了什么,if 语句和转换不起作用 - Please tell me what I'm doing wrong, the if statements and the conversions are not working 当尝试根据给定查询查找给定字符串中的子字符串计数时,有人能告诉我我在 Java 代码中做错了什么吗? - Can someone tell me what am I doing wrong here in my Java code when trying to find count of substrings in given string according to given queries? 有人可以告诉我for循环怎么了吗? - Can someone tell me what's wrong with the for loops? 有人可以告诉我for循环有什么问题吗 - Can someone please tell me what is wrong with the for loop 膨胀 class android.fragment.app.FragmentContainerView 时出错,有人可以告诉我我缺少什么 - Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing 我需要有人告诉我代码中的逻辑出了什么问题 - I need someone to tell me what is wrong with logic in my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM