How to find out if the list is full?

0

The method below is for adding a book to a list. Returns 1 if the book already exists in the list, 0 if it does not exist in the list and can be entered and 2 would be for full list. I'm trying to test if the list is full but I do not know how. Can someone 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 ;
        }
    }
}
    
asked by anonymous 12.12.2017 / 12:30

1 answer

7

Your question is a XY problem . You want to know how to determine if your list of books is full. But in fact your real problem is that you do not know how to define a list of books that is usable.

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

You would use 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.

Oh, 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 only one book.

    
15.12.2017 / 04:11