【问题标题】:A way to not allow duplicates to be shown on a arraylist without using the Hashset method一种在不使用 Hashset 方法的情况下不允许在数组列表上显示重复项的方法
【发布时间】:2016-01-28 04:53:34
【问题描述】:

所以我必须做出两种方法:

void setUnique( boolean value)

boolean getUnique()

setUnique允许客户端设置是否允许重复(true表示不允许重复,false表示允许重复,

getUnique 是返回唯一的当前设置

我的任务是我必须创建一个SortedIntList。 java 和 SortedIntListTest.java ,当我测试我的列表时,我必须包含这两种方法。

这是我目前所拥有的,我已经知道它不正确,因为它到处都有错误:

public void setUnique(boolean value)
{
      if(!list.contains(value))
      {
           list.add(value);
           return index == true;

      }
      else
      {

          return index == false;
      }
}

public boolean getUnique()
{
    //return value ;
}

现在我看到人们使用 hashset 方法。但是,我们还没有在课堂上学到这一点,所以很可能不允许使用它。我被困在这一点上,真的很想在另一种方式上得到帮助,现在允许在不使用 hashset 方法的情况下将重复项放入数组列表中

【问题讨论】:

  • 覆盖 add 方法。 Setter 和 getter 只是设置/获取字段,您应该像 private boolean allowsDuplicates 一样自我描述
  • 从您的代码看来,setUnique() 用于指示底层列表是否可以具有重复值,而 getUnqiue() 基本上表示当前状态。如果您最初允许重复但随后切换到唯一状态会发生什么情况,然后您会删除重复项吗?
  • 你们能写出一个程序示例吗?这样我就有了更好的主意。
  • 如果我正确理解了您的问题,您必须编写一个 SortedIntList 类,该类可以选择允许或不允许重复。正如@AnupamSaini 所述,当您从非唯一切换到唯一时会发生什么。

标签: java arraylist hashset sortedlist


【解决方案1】:

首先是上述方法 public void setUnique(boolean value) 它的返回类型是 void 但你返回的是一些错误的布尔值。

现在根据我的要求,将有一个布尔标志,指示客户端是否需要重复值。

所以在你的类中使用一个布尔变量“flag”并为该变量实现 setter 和 getter。客户将随时更改该标志。

现在创建一个返回列表的方法。

public List<Integer> getList(List<Integer> inputList){
List<Integer> list=new ArrayList<Integer>();
    if(flag){
       for(Intger i:inputList){
         if(!list.contains(i)){
          list.add(i);
         }
       }
    }
   else{
     list=inputList;
   }    
return list;
}

【讨论】:

    【解决方案2】:

    你可以这样写SortedIntList.java

    public class SortedIntList {
    
      private boolean isUnique;
      private ArrayList<Integer> arrayList = new ArrayList<>();
      public boolean isUnique() {
        return isUnique;
      }
    
      public void setIsUnique(boolean isUnique) {
        this.isUnique = isUnique;
      }
    
      public void  addElement(int element){
        //While inserting element, it will check whether isUnique is set or not    
    
        if(isUnique){
    
        //If set, then it will check whether the list contains that element, if Yes, then skip it.
            if(arrayList.contains(element)){
              return;
            }
        }
        arrayList.add(element);
    
      }
    
      // Add your custom methods for sorting purpose 
    
      public List getArray(){
        return arrayList;
      }
    }
    

    和SortedIntListTest.java这样的

    public class SortedIntListTest {
      public static void main(String[] args) {
    
        SortedIntList list = new SortedIntList();
        list.setIsUnique(true);
        list.addElement(10);
        list.addElement(5);
        list.addElement(10);
        list.addElement(8);
        System.out.println(list.getArray());
      }
    }
    

    【讨论】:

      【解决方案3】:

      根据输入,这里是要填写的模板代码。要在数组列表中查找重复项,请在列表中使用 contains 方法

       public class SortedIntList<Integer> extends AbstractList<Integer>{
      
          private List<Integer> myList = new LinkedList<Integer>
      
          private boolean isunique;
      
          public void add(int value){
          Case 1: if duplicates are allowed
          //add value to myList 
          Case 2: duplicates are not allowed
          while adding you need to traverse the list if there is any such value
          //sort the list
          }
      
          public void setUnique(boolean unique){
          //set isUnique
          //if value is changing from non-unique to unique
          //traverse the list if there is any duplicate values remove them
          }
      
          public boolean isUnique(){
            return isUnique;
          }
      
         }
      

      【讨论】:

        猜你喜欢
        • 2023-02-05
        • 2017-01-10
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        相关资源
        最近更新 更多