Je me demande à quoi ressembleraient les méthodes pour UnorderedList pour une ArrayList. Je sais que nous aurions addToFront, addToRear, et addAfter (tous avec des éléments T), mais je n'ai vu que ceci mis en œuvre avec un tableau (voir ci-dessous). Est-il possible d'utiliser un ArrayList à la place? Comment cela changerait les méthodes? J'espère que ma question a du sens.Java - Méthodes liste non ordonnée pour ArrayList?
C'est le code que je suis venu à travers:
public void addToFront(T element) {
if (size() == list.length) {
expandCapacity();
}
for (int i = this.size(); i > 0; i--) {
this.list[i] = this.list[i-1];
}
this.list[0] = element;
this.rear++;
}
/**
* Adds the specified element to the rear of this list.
*
* @param element the element to be added to the list
*/
public void addToRear(T element) {
if (size() == list.length) {
expandCapacity();
}
this.list[rear] = element;
this.rear++;
}
/**
* Adds the specified element after the specified target element.
* Throws an ElementNotFoundException if the target is not found.
*
* @param element the element to be added after the target element
* @param target the target that the element is to be added after
*/
public void addAfter(T element, T target) {
if (size() == list.length) {
expandCapacity();
}
int scan = 0;
while (scan < rear && !target.equals(list[scan])) {
scan++;
}
if (scan == rear) {
throw new ElementNotFoundException("list");
}
scan++;
for (int scan2 = rear; scan2 > scan; scan2--) {
list[scan2] = list[scan2 - 1];
}
list[scan] = element;
rear++;
}
}
Pas grand-chose. Essayez-le. Peu de représentation intégrée utilisée dans votre code peut changer. – nullpointer
@nullpointer Quand il s'agit d'augmenter la capacité, cela serait-il nécessaire pour une ArrayList? Mes connaissances peuvent être limitées, mais sa capacité ne se développe-t-elle pas automatiquement? –