简体   繁体   中英

can not compare two arraylist object

import java.util.ArrayList;
import java.util.List;

public class arr {
    public arr () {
// creat empty list
        List<Integer> alpha = new ArrayList<>();
    }
    public static void main (String []args ) {
        arr com1 = new arr();
        arr com2= new arr();
//returning false but should be true the two are empty array
        com1.equals(com2);
    }

}

I am trying to create a class that can build a an empty array but when I am trying to compare the two-object its returning false, but the two have an empty arraylist so it should return true

You are trying to call a missing method called isEmpty() .

You also need to move the list alpha as a field for the class, so that it can be accessed by the isEmpty() method.

To check for equality, just overload the equals() method.

Also as @Billy mentioned, you should override the hashCode too (so that it'll still work if you use hashSet/hashMap to store the arr class later on in your codes)

import java.util.ArrayList;
import java.util.List;

public class arr {
    List<Integer> alpha = new ArrayList<>(); 

    //creates the isEmpty method 
    boolean isEmpty() {return alpha.isEmpty();}

    //check if two arr classes are the same
    @Override
    public boolean equals(Object obj) {
       if (obj == this)   return true; 
       if (obj == null || obj.getClass() != this.getClass())   
           return false; 

       arr other = (arr) obj;
       //checks if the alpha of this is the same as the alpha in obj
       return  alpha.equals(other.alpha);
    }

    //also override the hashCode method so that it'll work correctly for hash containers like hashMap and hashSet.
    @Override
    public int hashCode(){ return alpha==null?0: alpha.hashCode(); }

    public static void main (String []args ) {
        arr com1 = new arr();
        arr com2= new arr();

       //calling the isEmpty() method from the main.
       System.out.println("com1 Empty? =" +com1.isEmpty());
       //checks if com1 and com2 are equal
       System.out.println("com1 same as com2? =" +com1.equals(com2));
    }

}

You can use size() method to check Arraylist size. Checkout this for further reference: https://www.geeksforgeeks.org/arraylist-size-method-in-java-with-examples/

Your code is syntactically wrong.

  • class arr

    • Java convention says class names should be nouns, in mixed case with the first letter of each internal word capitalized. So lets assume it is named as ArrCreator
  • arr() is a method, so:

    • it must have a return type
    • it cannot be accessed in a static way, should be through an instance of ArrCreator class
    • since it create an empty List, not a empty Array, let renamed it to createEmptyList()

Naming still messy, but now at lest you can see it compiling (I hope)

import java.util.ArrayList;
import java.util.List;    

public class ArrCreator
{    
    public List<Integer> createEmptyList()
    {
        // create empty list
        List<Integer> alpha = new ArrayList<>();
        return alpha;
    }

    public static void main(String[] args)
    {
        ArrCreator creator = new ArrCreator();

        List<Integer> com1 = creator.createEmptyList();
        List<Integer> com2 = creator.createEmptyList();

        System.out.println(com1.equals(com2));
    }
}

output: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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