Java: ArraryList, how to count the number of elements with the same instance variable?

时间:2016-04-04 17:46:42

标签: java arraylist count

Context: trying to write a programme that will store music albums.

So I have created an object called 'Album', the default constructor is as follows:

public Album(String artist, String title, int trackCount){
    this.artist = artist;
    this.title = title;
    this trackCount = trackCount
    status = null
}

The variable status refers to 3 possible statuses:

AVAILABLE; BROKEN; MISSING.

I then made an arraylist:

...
public class AlbumList(){
    albumList = new ArrayList<Album>();
}

I know that the .size() method will return the number of elements in my list.

However I am unsure how I would find the number of albums in my list that are missing.

I was thinking I should create a variable, say int missingAlbums, then from this maybe use an if statement to go through the array and increment missingAlbums each time an album is found with a status MISSING. Though I am not entirely sure how to do this.

Here is the code I have:

public int numberOfMissingAlbums(){
    missingAlbums= 0;
    for(Album missingRecord: AlbumList){
        if(missingRecord.status.equals("MISSING")){
            missingAlbums++;
        }
    }
    return missingAlbums;
}

I am getting an error saying '.equals on incompatible types', and I have no idea what the problem is.

Any help is appreciated.

5 个答案:

答案 0 :(得分:8)

Just iterate over the elements and check if the album is missing. If so, increment missingAlbums.

If you are using Java 8, you can also use streams like this: missingAlbums = albumList.stream().filter(album -> album.getStatus() == MISSING).count()

答案 1 :(得分:2)

You could try something like this

    int count = 0;
    for (int i = 0; i < albumList.size; i++) {
        if (albumList.get(i).equals("MISSING")) {
            count++;
        }

    }

答案 2 :(得分:1)

If the number of missing tracks is something that is going to need to be calculated repeatedly, it might be a better choice to make a wrapper class around the arraylist that also stores the number of missing albums. This would let you increment that number if a missing album was added to the list instead of having to iterate over the list every time.

Something like this:

public class Albums {

    List<Album> albums;
    int missing;

    public Albums() {
        albums = new ArrayList<>();
        missing = 0;
    }

    public void add(Album album) {
        albums.add(album);
        if (albums.getStatus().equals("MISSING")  {
            missing++;
        }
    }

    public int getMissingAlbums() {
        return missing;
    }
}

答案 3 :(得分:1)

You can iterate every element and get the object and find the status if it is MISSING then increment it

int missingAlbums = 0;
albumList = new ArrayList<Album>();
Iterator itr = albumList.iterator();
while(itr.hasNext()){
    Album temp = (Album)itr.next();
    if(temp.status.equals("MISSING"))
    missingAlbums++;
}
System.out.println(missingAlbums);

OR using for each loop

int missingAlbums= 0;
albumList = new ArrayList<Album>();
for(Album temp : albumList ){
    if(temp.status.equals("MISSING"))
    missingAlbums++;
}
System.out.println(missingAlbums);

答案 4 :(得分:1)

I propose using a method that will return a List of missing albums:

public static List<Album> getMissingAlbums(List<Album> currentAlbums) {
    List<Album> missingAlbums = new ArrayList<>();

    for (Album currAlbum : currentAlbums) {
        if (currAlbum.status.equals("MISSING")) {
            missingAlbums.add(currAlbum);
        }
    }

    return missingAlbums;
}

You can then call it like so:

    List<Album> missingAlbums = getMissingAlbums(albumList);

And get the count with List.size() like so:

    int missingAlbumCount = missingAlbums.size();

Now you have both the albums that are missing, and the count of albums that are missing.