Sequence list implementation exercise

1

I'm trying to solve this faculty exercise in which I have already implemented a good part, but it still lacks 3 things. It is about data structure with Java OO - sequential list.

Here is the code for the class in the list:

public class ListaSequencial {
    private Contato [] contatos = new Contato[10];
    private int n = 0;


    boolean isVazia(){
        return n == 0;
    }

    boolean isCheia(){
        return n == contatos.length;
    }

    int tamanho(){
        return n;
    }

    private void deslocaDireita(int pos){
        for(int i = n; i > pos;i--){
            contatos[i] = contatos[i - 1];
        }
    }

    boolean inserir(Contato contato){
        if(isCheia()){
            return false;
        }

        contatos[n] = contato;
        n++;
        return true;

    }
    boolean inserir(Contato c, int pos){
        if(isCheia()){
            return false;
        }
        deslocaDireita(pos);
        contatos[pos] = c;
        n++;
        return true;
    }

    private void deslocaEsquerda(int pos){
        for(int i = pos; i < n - 1 ;i++){
            contatos[i] = contatos[i+1]; 
        }
    }

    boolean remover(int pos){

        if(pos < 0 || pos >=n){
            return false;
        }
        deslocaEsquerda(pos);
        n--;
        return true;
    }


    public String imprimir(){
        String str = "";
        if(isVazia()){
            return "lista vazia";
        }
        else{
            for(int i = 0; i < n; i++){
                str += contatos[i].nome + " - " + contatos[i].telefone + "\n";   
            }
            return str;
        }
    }


    Contato buscar(int pos){
        if(pos < 0 || pos >= n){
            return null;
        }
        return contatos[pos];
    }

    int getPosicao(Contato contato){

        for(int i = 0; i < n; i++){
            if(contatos[i] == contato){
                return i;
            }
        }
        return -1;
    }
}

What I can not do and need for Exercise:

  • I need to create a method to concatenate 2 lists;

  • I need to create a method that as soon as the list pops up, create a larger container so that it never fills (type a loop);
  • Finally, I need a method to remove an element from the list by passing instead the index, the element itself. In this case, you remove the first occurrence found.

  • Someone help me?

        
    asked by anonymous 05.09.2016 / 00:43

    1 answer

    2

    (3) This is the easiest. Do the following:

    • Create the new method to delete the element, return is boolean .

    • Traverse the array using a for . The elements to be traversed are 0 to < n .

    • Check inside your for if apposition of the corresponding array has the element you are looking for. If so, use remover(int) to remove this position and return true soon after.

    • To decide whether the element found is what you are looking for or not, I recommend that you use the java.util.Objects.equals(Object a, Object b) method.

    • If you have finished traversing the array and can not find the element, return false .

    (2) Making the list grow is the most complicated of the three. Do the following:

    • Make the isCheia() method private. The reason for this is that from the external point of view of the class, the list never gets full, since when it fills, it will increase and no longer remain full, and therefore this method would no longer make sense to anyone who is looking at the class of out.

    • Create a private crescer() method, which is responsible for making the inner array grow in size. For this you create an array larger than the old one, copy all the elements and change the reference ( contatos = novoArray; ).

    • Instead of if (isCheia()) { return false; } , if (isCheia()) { crescer(); } .

    • The methods inserir return void .

    (1) To create the method that concatenates two lists do this:

    • This item is much easier if you've done part 2 first. Before implementing item 1, do item 2.

    • Create a static method that takes the two lists as a parameter and returns a list.

    • This method creates a new list. Scrolls through the lista1.n elements of the first list and adds them to the new list. Then it goes through% s of% elements of the second list and does the same thing.

    • Return to the new created list.

    05.09.2016 / 01:41