1 17 package org.apache.commons.collections.primitives; 18 19 import java.util.EmptyStackException ; 20 21 import org.apache.commons.collections.primitives.ArrayIntList; 22 23 30 public class IntStack 31 { 32 33 private ArrayIntList list = new ArrayIntList() ; 34 35 36 public IntStack() 37 { 38 } 39 40 41 public IntStack( int[] numbas ) 42 { 43 for ( int ii = 0; ii < numbas.length; ii++ ) 44 { 45 list.add( numbas[ii] ) ; 46 } 47 } 48 49 50 55 public boolean empty() 56 { 57 return list.isEmpty() ; 58 } 59 60 61 68 public int peek() 69 { 70 if ( list.isEmpty() ) 71 { 72 throw new EmptyStackException () ; 73 } 74 75 return list.get( list.size() - 1 ) ; 76 } 77 78 79 88 public int peek( int n ) 89 { 90 if ( list.isEmpty() ) 91 { 92 throw new EmptyStackException () ; 93 } 94 95 return list.get( list.size() - n - 1 ) ; 96 } 97 98 99 106 public int pop() 107 { 108 if ( list.isEmpty() ) 109 { 110 throw new EmptyStackException () ; 111 } 112 113 return list.removeElementAt( list.size() - 1 ) ; 114 } 115 116 117 123 public int push( int item ) 124 { 125 list.add( item ) ; 126 return item ; 127 } 128 129 130 140 public int search( int item ) 141 { 142 for ( int ii = list.size() - 1; ii >= 0; ii-- ) 143 { 144 if ( list.get( ii ) == item ) 145 { 146 return list.size() - ii ; 147 } 148 } 149 150 151 return -1 ; 152 } 153 154 155 163 public int get( int index ) 164 { 165 return list.get( index ) ; 166 } 167 168 169 174 public int size() 175 { 176 return list.size() ; 177 } 178 179 180 183 public void clear() 184 { 185 list.clear() ; 186 } 187 } 188 | Popular Tags |