Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class StackImpl {
- private int[] array = new int[10];
- int size = 0;
- public static void main(String[] args) {
- }
- public int size() {
- return size;
- }
- public boolean isEmpty() {
- return size == 0;
- }
- public void push(int element) {
- if(size == array.lenth) {
- // skapa ny lista med lenth*2 och lägga över alla element i samma ordning + det nya i slutet
- array = Arrays.copyOf(array, array.lenth*2); // tror det fungerar
- }
- else {
- //lägg till det nya elementet på plats size
- array[size] = element;
- }
- size++;
- }
- public int pop() {
- if(size <= 0) {
- throw new IndexOutOfBoundsException();
- } else {
- size--;
- return array[size];
- }
- }
- public int peek() {
- if(size <= 0) {
- throw new IndexOutOfBoundsException();
- } else {
- return array[size-1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement