How do you find out if the list is full?

Asked

Viewed 332 times

0

The method below is to include a book in a list. Returns 1 if the book already exists in the list, 0 if it does not exist in the list and can be inserted and 2 would be for full list. I’m trying to test if the list is full but I don’t know how. Can anyone help me?

public class ListaDeLivros
{
    private int proximoLivre;
    private int i;
    private Livros osLivrosDaLista[];
    private int capacidade;
    public ListaDeLivros(int proximoLivre, Livros osLivrosDaLista[],int capacidade){
        this.proximoLivre=proximoLivre;
        this.osLivrosDaLista=osLivrosDaLista;
        this.capacidade=capacidade;

    }

    public ListaDeLivros(int capacidade)
    {
        this.capacidade=capacidade;
        osLivrosDaLista=new Livros[capacidade];
    }

    public void setproximoLivre(int proximolivre){
        this.proximoLivre=proximolivre;
    }

    public int getproximoLivre(){
        return proximoLivre;
    }

    public void setcapacidade(int Capacidade){
        this.capacidade=Capacidade;

    }

    public int getcapacidade(){
        return capacidade;

    }

    public int ListaDeLivros(Livros livroAIncluir)
    {

        if(capacidade > proximoLivre)
        {
            for(i=0;i<proximoLivre;i++)
            {
                if(livroAIncluir.getTitulo().equals(osLivrosDaLista[i].getTitulo()))
                {
                    //System.out.println("titulo já existente!");
                    return 1;
                }

            }

            osLivrosDaLista[proximoLivre] = livroAIncluir ;
            proximoLivre++;
            return 0 ;
        }
    }
}

1 answer

8


Your question is a XY problem. You want to know how to determine if your list of books is full. But actually your real problem is that you don’t know how to define a list of books that is usable.

Using arrays like this, having to track which part was used or not or where you insert an element, whether or not it will burst and similar things is a painful, painful and unnecessary torture. Instead of suffering from it, use lists and everything will become easier. Just use the class ArrayList without having to add anything, that she already does all that you want and much more, with a spectacular performance and having already been tested, retested and exercised in every possible way zillions of times. Instead of reinvent the square wheel, use what the standard library already offers you.

You’d wear it like this:

Livro chapeuzinhoVermelho = ...;
Livro oPequenoPrincipe = ...;
Livro oMagicoDeOz = ...;

List<Livro> meusLivros = new ArrayList<>();
meusLivros.add(chapeuzinhoVermelho);
meusLivros.add(oPequenoPrincipe);
meusLivros.add(oMagicoDeOz);

System.out.println(meusLivros.size()); // Vai mostrar 3.

Ah, and note that I put the class name in the singular (Livro) and not in the plural (Livros). In general, it is good practice to use names in the singular, especially considering that each instance represents a book only.

Browser other questions tagged

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