Java Reference
In-Depth Information
1
/**
2
* Adds an item to this collection, at the end.
3
* @param x any object.
4
* @return true.
5
*/
6
public boolean add( AnyType x )
7
{
8
addLast( x );
9
return true;
10
}
11
12
/**
13
* Adds an item to this collection, at the front.
14
* Other items are slid one position higher.
15
* @param x any object.
16
*/
17
public void addFirst( AnyType x )
18
{
19
add( 0, x );
20
}
21
22
/**
23
* Adds an item to this collection, at the end.
24
* @param x any object.
25
*/
26
public void addLast( AnyType x )
27
{
28
add( size( ), x );
29
}
30
31
/**
32
* Adds an item to this collection, at a specified position.
33
* Items at or after that position are slid one position higher.
34
* @param x any object.
35
* @param idx position to add at.
36
* @throws IndexOutOfBoundsException if idx is not
37
* between 0 and size(), inclusive.
38
*/
39
public void add( int idx, AnyType x )
40
{
41
Node<AnyType> p = getNode( idx, 0, size);
42
Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p );
43
newNode.prev.next = newNode;
44
p.prev = newNode;
45
theSize++;
46
modCount++;
47
}
figure 17.24
add
methods for
standard
LinkedList
class
the node at index
idx
. In order for this to be suitable for
addLast
,
getNode
will
start its search from the end closest to the target node.
Search WWH ::
Custom Search