北工大咸鱼 2022-03-21 20:37 采纳率: 100%
浏览 20
已结题

run6行的调用哪里错了?怎么把date的数据放入栈(s)中

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

import java.util.ArrayList;
import java.util.Date;
import java.util.Stack;

public class DoubleStack<E> {
    public int bottom1;
    public int bottom2;
    public int top1;
    public int top2;
    private E[] date;
    public int m = 10;
    public int capacity;
    Stack<Integer> s = new Stack<Integer>();

    public DoubleStack() {
        date = (E[]) new DoubleStack[m];
        capacity = m;
        top1 = -1;
        top2 = capacity;

    }

    private boolean isFull(int no) {
        if (top1 == top2 - 1)
            throw new RuntimeException("栈满!");
        return false;//非空栈返回false;
    }

    private boolean isEmpty(int no) {
        if (no == 1) {
            if (top1 == -1) {
                throw new RuntimeException("此栈为空");
            }
            if (no == 2) {
                if (top2 == capacity) {
                    throw new RuntimeException("此栈为空");
                }
            }
        }

        return true;
    }

    public boolean Push(int no, E e) {
        if (top1 == top2 - 1) {
            throw new RuntimeException("栈满!");
        }
        if (no == 1) {
            top1++;
            date[top1] = e;
            System.out.println("元素" + e + "成功入栈1");
        } else if (no == 2) {
            top2--;
            date[top2] = e;
            System.out.println("元素" + e + "成功入栈2");
        }


        return true;
    }

    public E Pop(int no) {
        if (no == 1) {
            if (top1 == -1)
                throw new RuntimeException("栈1为空");
            return (E) date[top1--];
        } else if (no == 2) {
            if (top2 == capacity)
                throw new RuntimeException("栈2为空");
            return (E) date[top2++];
        }


        return null;
    }

    public void Show() {
        for (int i = 0; i < 10; i++) {
            s.push(i);
        }
        for (Integer x : s) {
            System.out.println(x);
        }
    }

    public String toString() {

        String str1 = "栈1:[";
        String str2 = "栈2:[";

        for (int i = top1; i >= 0; i--) {
            if (i == 0)
                str1 = str1 + (E) date[i];
            else
                str1 = str1 + (E) date[i] + ",";
        }

        str1 += "]";

        for (int i = top2; i < capacity; i++) {
            if (i == capacity - 1)
                str2 = str2 + (E) date[i];
            else
                str2 = str2 + (E) date[i] + ",";
        }

        str2 += "]";

        return str1 + "\n\r" + str2;
    }
}

import java.util.Stack;

public class run {
    public static void main(String[] args) {
        DoubleStack stack=new DoubleStack();
        stack.Push(1,1);
        stack.Push(1,3);
        stack.Push(1,5);
        stack.Push(1,7);
        stack.Push(1,9);
        stack.Push(2,2);
        stack.Push(2,4);
        stack.Push(2,6);

    }
}


  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2022-03-21 21:29
    关注

    run6是哪一行,看看报什么错。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 3月21日

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答