/**
* returns the element at the specified position in this list.
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws indexoutofboundsexception {@inheritdoc}
**/
public e get(int index) {
checkelementindex(index);
return node(index).item;
}
/**
* returns the (non-null) node at the specified element index.
**/
node<e> node(int index) {
// assert iselementindex(index);
if (index < (size >> 1)) {
node<e> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
node<e> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
下面是add方法,add方法把待添加的元素添加到链表末尾即可。
/**
* appends the specified element to the end of this list.
* <p>this method is equivalent to {@link #addlast}.
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link collection#add})
**/
public boolean add(e e) {
linklast(e);
return true;
}
/**
* links e as last element.
**/
void linklast(e e) {
final node<e> l = last;
final node<e> newnode = new node<>(l, e, null);
last = newnode;
if (l == null)
first = newnode;
else
l.next = newnode;
size++;
modcount++;
}
this is the end。 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/li_canhui/article/details/85004699