首页 \ 问答 \ ==或.Equals()(== or .Equals())

==或.Equals()(== or .Equals())

为什么要使用一个呢?


Why use one over the other?


原文:https://stackoverflow.com/questions/144530
更新时间:2023-11-02 08:11

最满意答案

你的代码很好,但你忘了检查所有元素,因为在找到第一个元素3的for循环中,它将进入中断状态,因此它不会检查其余的3s元素。 尝试这个代码,当你发现这个元素删除它并转到下一个时,你也不需要中断:

PS:你需要在每次删除过程后最小化它的计数变量,在你的代码中它将被执行一次。

public void remove(int value) {
    IntegerNode curr = head;
    IntegerNode prev = null;

        for (curr = head; curr != null; curr = curr.next) {
            if(curr.item == value) {
                if (prev == null) {
                    head = curr.next;
                } else {
                    prev.next = curr.next;
                }
                    count--;
            }
            prev = curr;
        }
}

your code is good but you forgot to check all elements because in for loop when the first element 3 is found will get into break , so it won't check the rest of 3s elements . try this code ,also you don't need break just when you find this element delete it and go to the next:

PS : The count variable you need to minimize it after every deletion process , in your code it will be executed just for one time .

public void remove(int value) {
    IntegerNode curr = head;
    IntegerNode prev = null;

        for (curr = head; curr != null; curr = curr.next) {
            if(curr.item == value) {
                if (prev == null) {
                    head = curr.next;
                } else {
                    prev.next = curr.next;
                }
                    count--;
            }
            prev = curr;
        }
}

相关问答

更多
  • 你的代码很好,但你忘了检查所有元素,因为在找到第一个元素3的for循环中,它将进入中断状态,因此它不会检查其余的3s元素。 尝试这个代码,当你发现这个元素删除它并转到下一个时,你也不需要中断: PS:你需要在每次删除过程后最小化它的计数变量,在你的代码中它将被执行一次。 public void remove(int value) { IntegerNode curr = head; IntegerNode prev = null; for (curr = head; cur ...
  • 第一个元素是一个特殊情况,因此您将拥有与其他情况略有不同的代码并不令人惊讶。 所以你的选择是(1)在你的例子中完成的方式,(2)将特殊情况代码放在主循环中,每次迭代执行if比较,或者(3)使用指针指针的不易理解的版本。 你应该问自己的第一个问题是:你为什么要重构? 为清楚起见,还是因为需要更快的实施? 要么 ...? The first element is a special case so it is unsurprising that you will have code for that one t ...
  • 由于你的代码检查了下一个元素,所以当你处于最后一个元素时,你需要停下来,如下所示: while (p != NULL && p->link != NULL) { ... } 获得条件第一部分的唯一原因是要捕获空列表。 另外,当你移除一个元素时,你不应该使指针前进。 否则,您将不会正确处理超过两个元素的运行。 Since your code examines the next element, you need to stop when you are at the element one befo ...
  • 列表理解是完美的。 [ k for k in list if int(k['value']) != x ] 您也可以使用filter ,但我相信列表理解在样式方面是首选: filter(lambda p: int(p['value']) != x, list) 编辑:注意到你的值是字符串,所以我添加了一个int转换。 A list comprehension is perfect for this. [ k for k in list if int(k['value']) != x ] You can ...
  • 要删除除第一个节点以外的所有节点,您可以尝试以下代码。 temp1 = head->next; while(temp1!=NULL) // as I am considering tail->next = NULL { head->next = temp1->next; temp1->next = NULL; free(temp1); temp1 = head->next; } 这将删除除第一个节点之外的所有节点。 但是第一个节点的数据将保持不变。 To delete ...
  • 1 - Java中没有标准的ListNode类。 2 - obj.next不是Node而是ListNode 。 你可以在评论字段中看到它。 ListNode类的成员接下来是ListNode类型。 实际上removeElements方法没有返回类似于: 1->2->3->3->4->5->3但它只返回1.所有元素如1,2,3,3,..都是ListNode你可以用下一个访问另一个。 1 - There is no standart ListNode class in Java. 2 - obj.next is ...
  • Java有自动垃圾回收功能,所以你只需要将Head引用设置为null: myList.headNode = null; 所以,假设我们有LinkedList类,它也有一个resetList函数... public class LinkedList{ private Node head; public Node find(Key k){ ... } public void add(Node n){ ... } ... public void reset(){ ...
  • 如果你以递归方式进行,那么就不需要迭代了。 将问题简化为一次迭代并将其应用于当前状态,然后将其应用于列表的其余部分。 似乎没有节点类,因此必须使用索引。 删除第三个元素意味着保留当前的两个元素并删除它们之后的元素。 我们还必须考虑到,当我们删除列表时,列表将“移动”,因此我们不必在删除后向前推进。 下一个'当前'将与我们删除的索引相同。 public void removeElements(List list, int current) { int removeIndex = current + 2; ...
  • 实际上并不可能做你想做的事情,因为在使用模板时你必须知道编译时涉及的类型 。 相反,遍历以前构建的链表需要您在运行时发现列表中的类型 。 为了说明,考虑一下: struct node_base { virtual ~node_base() {} } template struct node : public node_base { T data; node_base* right; } 现在您当然可以拥有node_base*列表,这些节点可以包含您想要的任 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。