HashSet usage with int arrays

后端 未结 3 893
说谎
说谎 2020-12-06 19:33

As I have an ArrayList of int arrays which contains duplicates, I\'d like to use HashSet. Unfortunately, I can\'t manage to use HashSet as I wish:

System.out         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 19:56

    Add each number individually. Don't add Arrays to HashSet

        int[] arr1 = {1,2,3};
        int[] arr2 = {1,2,3};
        System.out.println(arr1==arr2);//false
        System.out.println(arr1.equals(arr2)); //false
    

    Two arrays with same values need not be equal (they use default equals() method defined in Object which compares references.)

提交回复
热议问题