回答

收藏

如何实现在Java界栈10金钱.anslist{background:url(source/plugin/keke_ans

技术问答 技术问答 161 人阅读 | 0 人回复 | 2023-09-11

我需要帮助的事情是:
3 A+ s: m& K$ e) g' D* n8 u7 D/ X

    + D5 C$ |: Z) {9 d+ g
  • 什么是正确的代码,以使的isEmpty()和isFull()方法返回一个答案,告诉我,如果堆栈是空的,或者如果堆栈已满
    9 U5 ?4 o  ~% [  U: p
  • 我需要让我的筹码我使用的是五(5)的一组大小(堆栈可容纳5个整数)我需要在添加例外推()和pop()方法,即不让推时使用堆栈已满,或当栈为空弹出使用
    + X$ o0 M- [# Y6 D* {+ B% o7 y0 R
    0 L4 U0 A; X9 x  z% E6 m+ x5 Q: L! R
  • import java.util.ArrayList;) K/ e4 P7 r4 n, J/ g
  • import java.util.List;) S$ Z" a, U: K' X

  • 6 H4 [( R0 P( _2 L  s# L$ _
  • public class IntegerStack {
    & d) g! Q6 n: _+ R3 x: @

  • ( z6 o! a( s* X8 G& n/ W/ G
  •     private List<Integer> stack;
    & R9 V# X% s  \3 \& }

  • " w! O, N" T) `7 a  z" s
  •     public IntegerStack(int SIZE) {
    % M  j1 l$ s7 [1 h7 l( Z5 {
  •         stack = new ArrayList<Integer>(SIZE);
    4 T7 O; D; v" e( E/ U# ~
  •     }
    5 f' B, _  a+ |7 M
  • ( {2 E. f, X' Z% q1 U
  •     /* method to push the stack */( W* a6 D6 {* F
  •     public void push(int i) {2 R8 K3 A( S. H$ W! E+ d+ _

  • # z8 u  {! F3 e0 u. h
  •         stack.add(0, i);
    1 k' {2 J& Z, F1 S$ `+ l
  •     }2 V. h5 d  ^! f9 M, Q+ ~

  • $ J5 r& d9 u, x* I+ J4 l6 Q
  •     /* method to pop the stack */
    ' L" m4 k+ o$ E# d- t' t) ]
  •     public int pop() {
    * ^4 a/ L: x1 {+ ^
  •         if (!stack.isEmpty()) {# H+ l  W0 X- v2 m, C. _0 ]
  •             int i = stack.get(0);3 C# V/ x; S6 i' X! O) I% z1 D# J3 U
  •             stack.remove(0);
      D/ K; x0 V8 Q
  •             return i;
    & q. I4 A( B; z. r4 y& O4 D% L/ b
  •         } else {  E: u3 |* t" V9 ^
  •             return -1;// Or any invalid value9 @4 Z. c& J3 m
  •         }
    / M5 Q; g4 C& A# i" ^+ e
  •     }
    % e) a) z1 q! m/ Y

  • 5 d. e7 T) J. j0 J+ K9 C
  •     /* method to peek at the stack */3 r, M+ ~6 A" @& g4 s
  •     public int peek() {0 `3 a0 y+ z- c* r( T$ J% A
  •         if (!stack.isEmpty()) {
      P1 |7 N  i; p4 k
  •             return stack.get(0);
    ; [5 I+ k+ l9 A9 ^
  •         } else {9 X- g3 a' t" x* M
  •             return -1;// Or any invalid value
    ' d/ h# D/ z6 w. Y/ [, N6 H- Y
  •         }5 ^- P0 q) L3 s# F$ o. Y
  •     }
    ; R1 K. s- y, m% X
  • 9 n% T9 D; X( c) P1 A' w
  •     /* determine if the stack is empty */0 n9 c+ F2 C& V
  •     public boolean isEmpty() {
    7 ^( q" d( n: n' v+ ]4 y
  •         stack.isEmpty();( E: Y1 U2 I4 t1 s1 p! K# Y4 t' P
  •     }
    5 u. q) C2 o6 t0 A6 A& @
  • " @3 m( N9 T# q' M8 ^
  •     /* determine if the stack is full */9 ~) D% d  o) H# F' [
  •     public boolean isFull() {
    # c8 \+ [+ J" D* X( |! G0 j
  •         stack.isFull();
    ' F: G- s0 g: U
  •     }
    0 J/ z% z7 K+ M
  • 6 @( w2 s3 V/ m! a- u9 s6 ?5 _' l
  •     /* determine the size of the stack */; W% }' z- s7 t+ u; T  L6 y# B
  •     public int size() {
    + e; h; ]4 j- C0 g; s% c/ g
  •         if (stack.isEmpty())
    : p( R; s/ `6 Y2 Y
  •             return 0;/ {$ S! Z3 f& F1 c# p# |
  •         else9 W4 j3 _  I/ L( [
  •             return stack.size();( f1 H0 ^3 i( _. ?" m; Q3 w' b
  •     }0 ^5 y# e7 J! E$ g2 a
  • }
    ( W$ ]* ?/ \$ Y; u

; W- L/ `7 k$ L! H( x; n1 X- Z7 Q/ s! q3 _, _8 H' A
我知道答案 回答被采纳将会获得10 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则