Android (method for checking an arrays for repeated numbers?)

前端 未结 3 1881
时光取名叫无心
时光取名叫无心 2021-01-28 23:40

could any one help me. i am making an app, and in the java, numbers are send to a int array and i need to check if any of the numbers in the array repeated and if there are to c

3条回答
  •  星月不相逢
    2021-01-29 00:30

    What are yuou trying to emulate it's a SET

    A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

    Set is an interface so you can use HashSet or TreeSet to implement that interface. So you will have only 1 object of the same value even if you try to add the same.

    To ensure if you have already that object (in other to fire your event / method ) you can use the function contains that return a boolean checking if that element already exist in the set.

提交回复
热议问题