回答

收藏

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

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

我需要帮助的事情是:
- t! z8 @% ^$ ?, p( \% m+ U7 s+ G
    ( u, j# \5 ?: n5 l7 F
  • 什么是正确的代码,以使的isEmpty()和isFull()方法返回一个答案,告诉我,如果堆栈是空的,或者如果堆栈已满& r* ^! M  U) H/ _* C! A: [
  • 我需要让我的筹码我使用的是五(5)的一组大小(堆栈可容纳5个整数)我需要在添加例外推()和pop()方法,即不让推时使用堆栈已满,或当栈为空弹出使用
    " G, o6 t5 c- L3 \

    ) M. \$ D3 H/ P. I* O: B5 `2 _
  • import java.util.ArrayList;
    ( C* Z9 P9 a# ~3 P
  • import java.util.List;$ i, ^" @+ N( [6 M

  • 8 B5 a8 |/ y3 g3 J, O0 ?! `
  • public class IntegerStack {# c  M2 F. j6 W3 ?. X- f9 o' |
  • - v9 c; X* U+ _1 Z! Y8 e' b5 s( Q3 V
  •     private List<Integer> stack;" i' i6 I! b3 V- C  V( L

  • . Q4 V+ X" r. c! w
  •     public IntegerStack(int SIZE) {
    * `5 \% z: W8 c, ?) e: v+ u
  •         stack = new ArrayList<Integer>(SIZE);
    . M! x" l5 o3 b3 o0 i
  •     }7 b  B9 e+ P8 |6 C0 N! d4 b
  • 5 ^3 x! A+ S  B$ \1 v
  •     /* method to push the stack */
    3 d7 @$ W3 @2 K( f$ o# n: T
  •     public void push(int i) {4 }4 R4 j$ j2 w# J$ s: s

  • 0 M7 |4 J0 L8 q
  •         stack.add(0, i);% Z+ ]. @; V4 t6 l
  •     }% d& ?6 w/ u; i; g; r  V+ q1 A
  • $ D! y7 v& f8 S- f
  •     /* method to pop the stack */. P, ]7 E0 c* T8 o
  •     public int pop() {4 ~: ~  l5 u4 ]% _' r
  •         if (!stack.isEmpty()) {/ U( ^" Y8 Y+ I3 A* h
  •             int i = stack.get(0);
    6 u5 B. X% ?( \" `5 E( }* x
  •             stack.remove(0);
    6 f  r+ m" v7 ]' T
  •             return i;
    1 ?. Z; q* j, H4 N: O
  •         } else {
    4 `( x, }7 W5 v1 v- N
  •             return -1;// Or any invalid value; M) w# }- y9 x& U& C  t$ u/ K
  •         }1 R' q2 {% a) k7 E" u% S
  •     }
    + q$ m7 n7 W/ I' \+ s1 W2 U

  • 0 j5 s8 i( e  T3 C- _
  •     /* method to peek at the stack */
    4 v( _/ x0 }; l
  •     public int peek() {2 h& j) x7 ~. M: |
  •         if (!stack.isEmpty()) {
    . K- y) E: e+ }
  •             return stack.get(0);: m9 [5 V$ T" d
  •         } else {
    $ e: T2 \! {2 C/ [. ?  q8 x
  •             return -1;// Or any invalid value) z+ Y: m* x: g: A
  •         }
    ! ?# m  ?2 P7 A1 U+ f0 ~
  •     }7 A$ t; D7 ]& D

  • , B" |5 b; ^# f; O
  •     /* determine if the stack is empty */
    7 F4 T, B" C4 r8 r4 }- b9 O2 g# H0 g
  •     public boolean isEmpty() {' {2 I8 ~6 {1 E: ?8 B% u1 M; D" M  ?
  •         stack.isEmpty();3 _% l; t$ K2 F  A6 ^$ o4 o
  •     }
    6 L+ U/ A% X& L! l& h
  • ! ~1 U( T& ?( J: n; d" t+ ~
  •     /* determine if the stack is full */
    # C# K% H& t. A( ]
  •     public boolean isFull() {6 Y' @; g0 P, m
  •         stack.isFull();$ Z- t- z, V/ ^" d+ v9 d6 b
  •     }
    ; Z$ U' n( R9 I; T" a  x1 `
  • # Z6 r3 |& F3 o* F: h$ r
  •     /* determine the size of the stack */
    ( F4 I8 \) c6 u% ]
  •     public int size() {6 }4 M* s& Z+ Y9 [
  •         if (stack.isEmpty())5 x/ ^' r! `( ]0 G+ D
  •             return 0;
    ' b* k+ U0 n. X- m& k7 I
  •         else( z9 W5 m) j; I& V/ G; T8 J" a
  •             return stack.size();
    $ l. |6 G+ s1 M3 C6 }9 q
  •     }
    1 d) j$ [, V0 o9 y/ _  n8 Y) @
  • }0 A) k7 t7 O9 R. b# V
1 ]4 K$ h2 C# \  K5 K+ n

& ~3 T9 J$ F# |! i2 o( s$ R! e6 P9 L) }我知道答案 回答被采纳将会获得10 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则