java - 领英列表(java)

标签 java

“RemoveAll”类是链表类的一部分。我编写的类从链表中删除所有键,但不删除重复键。

有人知道为什么吗?我如何也可以删除重复的键?

public class LinkedIntList {

    private ListNode front; 
    private String name = "front"; 

    // Constructs an empty list.
    public LinkedIntList() {
        front = null;
    }


   public void removeAll(int key){

    if(front == null){ 

         throw new RuntimeException();

       }else if( front.data == (key)) {

      front = front.next;

      return;
   }

     ListNode cur  = front;

     ListNode prev = null;

   while(cur != null  && cur.data != (key) ){

      prev = cur;

      cur = cur.next;
   }

   if(cur == null) 

    throw new RuntimeException();

   
   prev.next = cur.next;

}

最佳答案

如果您有重复项,一种蛮力方法是继续调用 removeAll() 直到它返回 false。

while (myCollection.removeAll(someOthercollection))
   ;  // comment here that the loop does nothing for clarity

添加:正如@Dukeling 正确指出的那样,您不必这样做。但是,如果 removeALL() 实现不当,您可能不得不这样做。

关于java - 领英列表(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193262/

相关文章:

java - 在 Spring 中使用 JAX-WS 对 Web 服务客户端进行摘要式密码身份验证

java - 我可以使用 File.createTempFile() 创建具有非随机名称的文件吗

java - 将 GMT 字符串解析为本地

Java邮件 : can FetchProfile be used when searching mails instead of just fetching them?

java - 如何在Jhipster中配置热重载?

java - MVC : how should I switch between different datasets - update or replace?

java - JDK 1.5 中的 MessageFormat.format 和 String.format 有什么区别?

java - 通用类输入参数类型不匹配

java - 为什么 Spring Boot MVC 总是从我的表单返回 null 值?

java - graphql-spqr 中 Double 值的 NaN 模型