简体   繁体   English

一种不使用Hashset方法而不允许在数组列表中显示重复项的方法

[英]A way to not allow duplicates to be shown on a arraylist without using the Hashset method

So I have to make two methods: 因此,我必须采用两种方法:

void setUnique( boolean value)

boolean getUnique()

setUnique allows the client to set whether or not to allow duplicates( true means no duplicates, false means duplicates allowed, setUnique允许客户端设置是否允许重复(true表示不重复,false表示允许重复,

getUnique is to return the current setting for unique getUnique将返回当前设置为唯一

My assignment is I have to create a SortedIntList . 我的任务是我必须创建一个SortedIntList java and a SortedIntListTest .java and I have to have these two methods included for when I test my list. java和SortedIntListTest .java,在测试列表时必须包含这两个方法。

This is what I have so far and I already know its not correct as it has errors all over it: 这是我到目前为止所拥有的,并且我已经知道它不正确,因为它到处都有错误:

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

      }
      else
      {

          return index == false;
      }
}

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

Now I've seen people use the hashset method. 现在,我已经看到人们使用哈希集方法。 However we havnt learn that in class yet so it most likely wont be allowed to be used. 但是,我们还没有在课堂上学到它,因此很可能将不允许使用它。 I am stuck on this and would really like some help on another way of now allowing duplicates into the arraylist without using the hashset method 我一直坚持这一点,真的希望以其他方式提供一些帮助,现在允许不使用hashset方法将重复项放入arraylist

first of all above method public void setUnique(boolean value) it's return type is void but you are returning some boolean value whichis wrong. 首先,以上方法public void setUnique(boolean value)其返回类型为void,但您返回的是布尔值,这是错误的。

now as per me your requirement there will be one boolean flag which indicate whether client wants duplicate values or not. 现在按照我的要求,将有一个布尔标志,指示客户是否要重复值。

so in your class take one boolean variable "flag" and implement setter and getter for that variable. 因此,在您的类中使用一个布尔变量“ flag”,并为该变量实现setter和getter。 client will change that flag whenever he want to change. 客户只要想更改,便会更改该标志。

now create one method which return List. 现在创建一个返回List的方法。

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;
}

You can write SortedIntList.java like this 您可以这样编写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;
  }
}

And SortedIntListTest.java like this 和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());
  }
}

Based on the inputs, here is the template code to fill in. To find duplicates in array list, use contains method on list 根据输入,这里是要填写的模板代码。要在数组列表中查找重复项,请使用list上的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;
    }

   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM