Java by API/java.util/Stack
Содержание
new Stack()
/*
* Output:
*
stack: []
push(42)
stack: [42]
pop -> 42
stack: []
empty stack
*
*/
import java.util.Stack;
import java.util.EmptyStackException;
public class MainClass {
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack: " + st);
st.push(new Integer(42));
System.out.println("push(" + 42 + ")");
System.out.println("stack: " + st);
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
try {
st.pop();
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
new Stack < E > ()
/**
*Output:
stack: []
push(2)
stack: [2]
push(6)
stack: [2, 6]
push(9)
stack: [2, 6, 9]
pop -> 9
stack: [2, 6]
pop -> 6
stack: [2]
*/
import java.util.EmptyStackException;
import java.util.Stack;
public class MainClass {
static void showpush(Stack<Integer> st, int a) {
st.push(a);
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack<Integer> st) {
System.out.print("pop -> ");
Integer a = st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[]) {
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
showpush(st, 2);
showpush(st, 6);
showpush(st, 9);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
Stack: empty()
import java.util.Stack;
public class Main {
public static void main (String args[]) {
Stack<String> s = new Stack<String>();
s.push("A");
s.push("B");
s.push("C");
System.out.println(s.pop());
System.out.println(s.empty());
}
}
Stack: peek()
import java.util.Stack;
public class Main {
public static void main (String args[]) {
Stack<String> s = new Stack<String>();
s.push("A");
s.push("B");
s.push("C");
System.out.println("Next: " + s.peek());
}
}
Stack: pop()
/*
* Output:
*
stack: []
push(42)
stack: [42]
pop -> 42
stack: []
empty stack
*
*/
import java.util.Stack;
import java.util.EmptyStackException;
public class MainClass {
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack: " + st);
st.push(new Integer(42));
System.out.println("push(" + 42 + ")");
System.out.println("stack: " + st);
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
try {
st.pop();
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
Stack: push(E item)
/*
* Output:
*
stack: []
push(42)
stack: [42]
pop -> 42
stack: []
empty stack
*
*/
import java.util.Stack;
import java.util.EmptyStackException;
public class MainClass {
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack: " + st);
st.push(new Integer(42));
System.out.println("push(" + 42 + ")");
System.out.println("stack: " + st);
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
try {
st.pop();
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
Stack: search(Object o)
import java.util.Stack;
public class Main {
public static void main (String args[]) {
Stack<String> s = new Stack<String>();
s.push("A");
s.push("B");
s.push("C");
System.out.println("Next: " + s.peek());
s.push("D");
System.out.println(s.pop());
s.push("E");
s.push("F");
int count = s.search("E");
while (count != -1 && count > 1) {
s.pop();
count--;
}
System.out.println(s);
}
}