Count elements in a Java Arraylist

Asked

Viewed 7,833 times

7

I have a Arraylist calling for tracklist within the class Album, that contains a number of songs. I need to go through and return how many songs are within the album through a class I named contarMusica. For now I’ve done something like:

public class Album {
private int id;
private String Nome_album; 
Date Lancamento;
private Musica [] tracklist = new Musica[30];
static private int ultimo = 1;

//Contructor
public Album(int id, String nome_album, Date lancamento, Musica[] tracklist) {
    id=ultimo;
    ultimo++;
    Nome_album = nome_album;
    Lancamento = lancamento;
    this.tracklist = tracklist;
}


public void contarMusica(Musica m){

    int contador = 0;

            ...
}
  • Your class will only have this Builder? if yes you do not need to give a new Music[30] and then by Length you take the amount of music

  • 3

    In Java arrays are different from Arraylists. In your code you are using arrays and not Arraylists. Internally Arraylists use arrays to store their objects, but Arraylists are dynamic, that is, increase and decrease in size while arrays have fixed size.

  • Yes, this class only has this Constructor. I thank you for the clarification Piovezan and the response of Fccdias! I’m kind of new in Java yet.

3 answers

6


You can use the attribute length, and you can access it to know the size of the array.

public void contarMusica(Musica m){

 int contador = 0;
 for (int i = 0; i < m.length; i ++)
 if (m[i] != null)
    contador ++;
 }

1

You could use it like this (Create the Get/Set and the same are already ready for various functionalities)

Class:

public class Album {
    private int id;
    private String Nome_album; 
    Date Lancamento;
    private Musica[] tracklist;
    static private int ultimo = 1;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNome_album() {
        return Nome_album;
    }
    public void setNome_album(String Nome_album) {
        this.Nome_album = Nome_album;
    }
    public Musica[] getTracklist() {
        return tracklist;
    }
    public void setTracklist(Musica[] tracklist) {
        this.tracklist = tracklist;
    }
    public static int getUltimo() {
        return ultimo;
    }
    public static void setUltimo(int ultimo) {
        Album.ultimo = ultimo;
    }
    public Album(int id, String nome_album, Date lancamento, Musica[] tracklist) {
        id=ultimo;
        ultimo++;
        Nome_album = nome_album;
        Lancamento = lancamento;
        this.tracklist = tracklist;
    }
}

In the Codification:

Musica[] musica = new Musica[10];        
Album album = new Album(1, null, null, musica);

int totalMusica = album.getTracklist().length; // quantidade de música

I would do so:

Classes

public class Musica {
    private int id;
    private String nome;
    private int ano;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public int getAno() {
        return ano;
    }
    public void setAno(int ano) {
        this.ano = ano;
    }   
}

package classes;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Album {
    private int id;
    private String nomeAlbum; 
    private Date lancamento;
    private List<Musica> tracklist;
    static private int ultimo = 1;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNomeAlbum() {
        return nomeAlbum;
    }
    public void setNomeAlbum(String nomeAlbum) {
        this.nomeAlbum = nomeAlbum;
    }
    public Date getLancamento() {
        return lancamento;
    }
    public void setLancamento(Date lancamento) {
        this.lancamento = lancamento;
    }
    public List getTracklist() {
        return tracklist;
    }
    public void setTracklist(List tracklist) {
        this.tracklist = tracklist;
    }
    public static int getUltimo() {
        return ultimo;
    }
    public static void setUltimo(int ultimo) {
        Album.ultimo = ultimo;
    }
    //Contructor
    public Album(){
        this.tracklist = new ArrayList<>();
    }
    public Album(int id, String nomeAlbum, Date lancamento, List tracklist) {
        this.id = ultimo;        
        this.nomeAlbum = nomeAlbum;
        this.lancamento = lancamento;
        this.tracklist = tracklist;
    }
}

Testing

import classes.Album;
import classes.Musica;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JavaApplication1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {           
       Musica music1 = new Musica();
       Musica music2 = new Musica();
       List<Musica> musicas = new ArrayList<>();
       musicas.add(music1);
       musicas.add(music2);       
       Album album = new Album(1, "Nome Album 1", new Date() , musicas);
       album.getTracklist().size(); // quantidade que estão na lista
    }
}

'Cause I’d do it like this?

Because, the List is dynamic so that you can add/remove/change songs from a particular album, one album may have more songs than the other and limit the list would be a mistake ...

-1

int size = tracklist.lenght;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.