Utilizando as ideias para implementação de tabelas Hash apres...

Utilizando as ideias para implementação de tabelas Hash apresentadas nas aulas, necessitamos uma lógica correta para o método toString, que retornará o conteúdo completo da tabela Hash, ou seja, retornará os dados de todos os objetos nela armazenados.public String toString() {

}

Lembremos que a tabela Hash era guardada em um ArrayList, atributo da classe:

private ArrayList hashtable[];

Este método deverá mostrar os objetos que temos em cada lista encadeada. Por exemplo, se adicionamos os objetos a seguir na tabela hash:

tabhash. adiciona(new Aluno(1945, "Luis Alves", 21, 'M'));

tabhash. adiciona(new Aluno(1918, "Ana Lopes", 24, 'F'));

tabhash. adiciona(new Aluno(1492, "Bia Lima", 31, 'F'));

tabhash. adiciona(new Aluno(1776, "Henrique Silva", 44, 'M'));

tabhash. adiciona(new Aluno(1812, "Renata Peres", 19, 'F'));

tabhash. adiciona(new Aluno(1055, "Pedro Batista", 35, 'M'));

tabhash. adiciona(new Aluno(2017, "Caio Souza", 25, 'M'));

Lembrando que a função de Hash era ((5 * chave) % 8), o método toString() deverá retornar:

pos: 0

1776, Henrique Silva, M, idade: 44

pos: 1

pos: 2

pos: 3

1055, Pedro Batista, M, idade: 35

pos: 4

1492, Bia Lima, F, idade: 31

1812, Renata Peres, F, idade: 19

pos: 5

1945, Luis Alves, M, idade: 21

2017, Caio Souza, M, idade: 25

pos: 6

1918, Ana Lopes, F, idade: 24

pos: 7

Em sua opinião, qual seria uma implementação correta para o método toString?

public String toString() {

String str = "";

for (int i = 0; i < hashtable. size(); i++) {

str += " pos: " + i + " ";

for (int j = 0; j < hashtable[i].lenght; j++) {

str += hashtable[i].get(j) + " ";

}

}

return str;

}

public String toString() {

String str = "";

for (int i = 0; i < hashtable. length; i++) {

str += " pos: " + i + " ";

for (int j = 0; j < hashtable[i].size(); j++) {

str += hashtable[i].get(j) + " ";

}

}

return str;

}

public String toString() {

String str = "";

for (int i = 1; i <= hashtable. length; i++) {

str += " pos: " + i + " ";

for (int j = 1; j <= hashtable[i].size(); j++) {

str += hashtable[i].get(j) + " ";

}

}

return str;

}

public String toString() {

String res = "";

for (int i = 0; i < hashtable. length; i++) {

res += " pos: " + i + " ";

for (int j = 0; j < hashtable. size(); j++) {

res += hashtable[i].get(j) + " ";

}

}

return res;

}

public String toString() {

String str = "";

for (int j = 0; j < hashtable. length; j++) {

str += " pos: " + j + " ";

for (int i = 0; i < hashtable[j].size(); i++) {

str += hashtable[i].get(j) + " ";

}

}

return str;

}

public String toString() {

String str = " pos: ";

for (int j = 0; j < hashtable[i].size(); j++) {

str += hashtable[i].get(j) + " ";

}

return str;

}

0 Respostas

Rozanamacedo Gael está aguardando sua ajuda.

Sua resposta
Ok

Mais perguntas de Informática





















Toda Materia
Toda Materia
Toda Materia

Você tem alguma dúvida?

Faça sua pergunta e receba a resposta de outros estudantes.

Escola Educação