I have this assignment so I'm not looking for an answer but I just want to understand basically how can we compare two variables of the same generic type in Java using the equals method. I've searched stackoverflow for some answers but only could find using the compareTo method where your class extends Comparable I think.
package Maillon;
import java.util.ArrayList;
public class ListeAssociativeChainee<K, E> implements IListeAssociative <C, E> {
private Node<K> keys;
private Node<Node<E>> elements;
private int nbrKeys;
public ListeAssociativeChainee () {
keys = new Node<>(null);
elements = new Node<>(null);
nbrKeys = 0;
}
}
What it currently looks like:
What I am trying to do:
My problem hits when I try to delete an element by looking for it through the associative list with the key and element given in the parametres such as:
public boolean supprimer(K key, E element) {
/*****/
}
However, I also have another overloaded method that goes like this:
public boolean supprimer(K key, int index) {
/*****/
}
This one says that it deletes an element at the index specified using the key and an index point given in parametres.
Now say that I create an associative list as follow:
IListAssociative<String, Integer> testList = new ListAssociativeChainee();
where IListAssocivative is an interface implemented in ListAssociativeChainee.
now if my list contains values of type int as follow:
key1 -> [1, 2, 4, 5, 6]
key2 -> [5, 6, 8]
key3 -> [0, 7, 23]
and I want to use the delete method
testList.supprimer(K key, E element);
and I say:
testList.supprimer("key3", 23);
instead it's going to call
testList.supprimer(K key, int index);
and that's because I'm using an int and not an Integer from what I understand.
now say that I do this:
testList.supprimer("key3", new Integer(23));
it will call
testList.supprimer(K key, E element);
but will look for values of type Integer that contains 23.
however, my method elementExists()
will always return false in that case.
Any tips on how I could do it?
EDIT:
here's my elementExists()
private boolean elementExiste(Node<Node<E>> listeElements, E element)
{
Node<Node<E>> m = listeElements;
Node<E> tmp = m.info();
boolean existe = false;
while(tmp != null && tmp.info() != element) {
tmp = tmp.next();
}
if(tmp != null && tmp.info() == element) {
existe = true;
}
return existe;
}
I know that my problem here is that I'm using ==
to compare and int and an Integer and this is wrong but I'm not sure how to implement the equals()
method to compare
When your code provides ambiguous methods like your two delete, the best solution is always to rename the method themselves. Instead of supprimer(Key, int)
, rename it to supprimerParIdx(Key, int)
.
You could also check the instance of
your Object to make sure it's not an Integer
:
if (element instanceof Integer) {
throw new RuntimeException("Integers are forbidden by my law!");
}
or instead of an Exception, unbox and call the other method
this(key, element.intValue());
This is, of course, dirty in everyway.
On a side note :
new Integer(23)
: Never do this, use Integer.valueOf(23)
instead, same goes for most Boxed primitives.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.