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;
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?