Java Reference
In-Depth Information
Figure 17.25 details the various
get
methods, plus a
set
method. There is
little special in any of those routines. The
element
method from the
Queue
interface is not shown. Figure 17.26 has the previously mentioned private
1
/**
2
* Returns the first item in the list.
3
* @throws NoSuchElementException if the list is empty.
4
*/
5
public AnyType getFirst( )
6
{
7
if( isEmpty( ) )
8
throw new NoSuchElementException( );
9
return getNode( 0 ).data;
10
}
11
12
/**
13
* Returns the last item in the list.
14
* @throws NoSuchElementException if the list is empty.
15
*/
16
public AnyType getLast( )
17
{
18
if( isEmpty( ) )
19
throw new NoSuchElementException( );
20
return getNode( size( ) - 1 ).data;
21
}
22
23
/**
24
* Returns the item at position idx.
25
* @param idx the index to search in.
26
* @throws IndexOutOfBoundsException if index is out of range.
27
*/
28
public AnyType get( int idx )
29
{
30
return getNode( idx ).data;
31
}
32
33
/**
34
* Changes the item at position idx.
35
* @param idx the index to change.
36
* @param newVal the new value.
37
* @return the old value.
38
* @throws IndexOutOfBoundsException if index is out of range.
39
*/
40
public AnyType set( int idx, AnyType newVal )
41
{
42
Node<AnyType> p = getNode( idx );
43
AnyType oldVal = p.data;
44
45
p.data = newVal;
46
return oldVal;
47
}
figure 17.25
get
and
set
methods
for standard
LinkedList
class
Search WWH ::
Custom Search