Count elements in an ArrayList in Java

7

I have Arraylist called tracklist within class Album , which contains a number of songs. I need to loop through and return how many songs I have inside the album through a class I named contarMusica . For now I've done something like this:

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;

            ...
}
    
asked by anonymous 21.04.2014 / 19:08

3 answers

6

You can use the 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 ++;
 }
    
21.04.2014 / 19:13
1

You could use it like this (Create Get / Set and they're ready for various features)

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 Encoding:

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 this:

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
    }
}

Why would I do this?

Because List is dynamic so you can add / remove / change songs from a given album, one album being able to have more songs than the other and limiting the list would be an error ...

    
21.04.2014 / 19:47
-1

int size = tracklist.lenght;

    
06.10.2017 / 02:30