-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIntStack2.java
More file actions
33 lines (24 loc) · 1022 Bytes
/
IntStack2.java
File metadata and controls
33 lines (24 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package chapter9;
//Another version of IntStack that has a private interface method that is used by two default methods.
public interface IntStack2 {
void push(int item); //store an item
int pop(); //retrieve an item
//A default method that returns an array that contains the top n elements on the stack
default int[] popNElements(int n) {
//Return the requested elements.
return getElements(n);
}
//A default method that returns an array that contains the next n elements on the stack after skipping elements
default int[] skipAndPopNElements(int skip, int n) {
//Skip the specified number of elements
getElements(skip);
//Return the requested elements
return getElements(n);
}
//A private method that returns an array containing the top n elements on the stack
private int[] getElements(int n) {
int[] elements = new int[n];
for (int i = 0; i < n; i++) elements[i] = pop();
return elements;
}
}