0
I have a data structure work in which I need to create an array and implement the interface Map
of Java and, inside it, I store the ordered maps. I’ve already created the vector, but now I don’t know how to implement the method put
(and others).
package vetor;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import estudante.Estudante;
public class Vetor_map implements Map<Object, Object> {
private int nElementos;
private Map mapa[];
public Vetor_map(int max) {
nElementos = 0;
}
// PUT
@Override
public Object put(Object key, Object value) {
if (!isFull()) {
mapa[nElementos].put(key, value);
nElementos++;
return true;
}
return false;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
if (nElementos == 0)
return true;
return false;
}
public boolean isFull() {
if (nElementos == mapa.length) {
return true;
}
return false;
}
//tem outros metodos abaixo mas nao implementei ainda
Down with main
:
package main;
import estudante.Estudante;
import vetor.Chave;
import vetor.Vetor_map;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vetor_map vm = new Vetor_map(10);
Chave ch = new Chave();
Estudante es = new Estudante();
System.out.println("Chave: " + ch + ", Estudante: "+ es);
vm.put(ch, es);
System.out.println(vm.get(ch));
}
}
I don’t find any example of similar implementation on the internet.
this occurs pq inside its Vetor_map class the map variable is not initialized
– Lucas Miranda
I fixed that part in the constructor and it follows the same error, I believe it is the put, but I do not know how to solve
– Matheus Zalamena
you initialized the array maps as well? if you do for example Map[] = new Map[10], the array will be ok but the internal maps will still be null, you need then initialize the index map too, for example map[0] = new Hashmap(); and then yes give the put
– Lucas Miranda