Java - ArrayList 的无序列表方法?
Java - Unordered List methods for ArrayList?
我想知道 ArrayList 的 UnorderedList 的方法是什么样的。我知道我们会有 addToFront、addToRear 和 addAfter(都带有 T 元素),但我只见过用数组实现的(见下文)。是否可以改用 ArrayList?这将如何改变方法?希望我的问题有道理。
这是我遇到的代码:
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++;
}
}
ArrayList 抽象了过渡数组的大小管理方面。查看:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int,%20E)
import java.util.ArrayList;
public class Test<T> {
ArrayList<T> yourArray;
Test() {
// Initialize to avoid NPEs
yourArray = new ArrayList<T>();
}
public void addToFront(T element) {
yourArray.add(0, element);
}
public void addToRear(T element) {
yourArray.add(yourArray.size(), element);
}
public void addAfter(T element, T target) {
yourArray.add(yourArray.indexOf(target) + 1, element);
}
public void addBefore(T element, T target) {
final int location = yourArray.indexOf(target);
if (location == 0) {
addToFront(element);
} else {
yourArray.add(yourArray.indexOf(target) - 1, element);
}
}
}
我想知道 ArrayList 的 UnorderedList 的方法是什么样的。我知道我们会有 addToFront、addToRear 和 addAfter(都带有 T 元素),但我只见过用数组实现的(见下文)。是否可以改用 ArrayList?这将如何改变方法?希望我的问题有道理。
这是我遇到的代码:
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++;
}
}
ArrayList 抽象了过渡数组的大小管理方面。查看:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int,%20E)
import java.util.ArrayList;
public class Test<T> {
ArrayList<T> yourArray;
Test() {
// Initialize to avoid NPEs
yourArray = new ArrayList<T>();
}
public void addToFront(T element) {
yourArray.add(0, element);
}
public void addToRear(T element) {
yourArray.add(yourArray.size(), element);
}
public void addAfter(T element, T target) {
yourArray.add(yourArray.indexOf(target) + 1, element);
}
public void addBefore(T element, T target) {
final int location = yourArray.indexOf(target);
if (location == 0) {
addToFront(element);
} else {
yourArray.add(yourArray.indexOf(target) - 1, element);
}
}
}