【问题标题】:Stack sub Stack creation堆栈子堆栈创建
【发布时间】:2018-01-30 03:40:30
【问题描述】:

如何在堆栈中创建堆栈?喜欢

[[1 2 3][3 4 5][6 7 8]]

所以有三个子堆栈,例如 [123][456][789],它们位于一个主堆栈中。如何在 Java 中使用 stack build in 函数创建它?

【问题讨论】:

  • 您将此问题标记为ArrayListStack。是哪个?
  • 不清楚你想要什么。当您弹出外部堆栈时,您是否希望将整个子堆栈作为Stack 对象接收?那只是一个Deque<Deque<Integer>>。如果这不是您想要的,您必须澄清pushpop 上发生的情况。
  • 什么意思:“堆栈构建”?
  • 我需要在堆栈中创建一个堆栈,然后想要对主堆栈进行排序和反转。

标签: java collections stack


【解决方案1】:

基于第一个元素表示 1,4,7。顺序将是 7、4、1,但其他元素完好无损。所以排序后的结果是[[7,8,9],[4,5,6],[1,2,3]]。如果我的堆栈是这样的,比如[[1,10,5],[5,11,15],[4,9,2]]。排序后,我得到类似 [[5,11,15],[4,9,2],[1,10,5]] 的结果。所以这意味着只有第一个元素将被排序,其余部分相同。

我认为您可以使用 Comparator(来自 Java 8 它是 @FunctionalInterface)例如:

    Stack<Stack<Integer>> stacks = new Stack<>();
    // Previous example
    // ...
    //straight Comparator
    Comparator<Stack<Integer>> byFirst = Comparator.comparingInt(Stack::firstElement);
    //reverse Comparator
    Comparator<Stack<Integer>> reverseByFirst 
           = (Stack<Integer> s1, Stack<Integer> s2)
                                   -> s2.firstElement().compareTo(s1.firstElement());
    // or  = (Stack<Integer> s1, Stack<Integer> s2)-> s2.get(0).compareTo(s1.get(0));

    // get Stack
    stacks = Stream.of(stackI1,stackI2,stackI3)
    //          .sorted(byFirst)       // byFirst
              .sorted(reverseByFirst)  // reverse
              .collect(Collector.of(
                           () -> new Stack(),
                           (s,b)->s.push(b),
                           (b1, b2) -> (Stack) b1.push(b2)
                       )
                  );
    System.out.println(stacks); //[[7, 8, 9], [4, 5, 6], [1, 2, 3]]

我认为它有帮助......

【讨论】:

  • 感谢您的建议。但它适用于第一个元素和最后一个元素。中间一如往常。[[2, 10, 5], [10, 5, 6], [5, 15, 9]] [[5, 15, 9], [10, 5, 6], [2, 10, 5]] 进程以退出代码 0 结束
  • 我会尝试检查)))
  • 好的,谢谢您的帮助
  • 我检查了 => stack.peek() - 看这里)))
【解决方案2】:

您可以使用 lambda 表达式(如果我理解您的问题正确):

    Stack<Integer> stackI1 = new Stack<>();
    stackI1.push(1);
    stackI1.push(2);
    stackI1.push(3);

    Stack<Integer> stackI2 = new Stack<>();
    stackI2.push(4);
    stackI2.push(5);
    stackI2.push(6);

    Stack<Integer> stackI3 = new Stack<>();
    stackI3.push(7);
    stackI3.push(8);
    stackI3.push(9);

    Stack<Stack<Integer>> stacks = new Stack<>();
    stack.push(stackI1);
    stack.push(stackI2);
    stack.push(stackI3);
    System.out.println(stacks); //[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    /**
     *  lambda expressions
     */
    // stacks = stacks.stream() 
    stacks = Stream.of(stackI1,stackI2,stackI3)       
                  .collect(Collector.of(
                               () -> new Stack(),
                               (s,b)->s.push(b),
                               (b1, b2) -> (Stack) b1.push(b2)
                           )
                      );
    System.out.println(stacks); //[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

也许有帮助:example => Collector.of(...)

【讨论】:

  • 这段代码运行完美。而 Stack> stack = new Stack(); 的含义是什么?表达。但是,如果我想对堆栈 [stack] 进行排序和反转,那么内置函数 Java 是什么?我尝试了 Collection.sort,但它不起作用。它显示了两个函数 Collection.lamda 或 Collection.class。我该如何继续?
  • 您希望如何对 Stack 进行排序:如 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [[9,8,7], [6,5,4], [3,2,1]][[1, 2, 3], [4, 5, 6], [7, 8, 9]] => @ 987654326@,要不然呢?
  • 基于第一个元素表示 1,4,7。顺序将是 7、4、1,但其他元素完好无损。所以排序后的结果是[[7,8,9],[4,5,6],[1,2,3]]。如果我的堆栈是这样的,比如[[1,10,5],[5,11,15],[4,9,2]]。排序后,我得到类似 [[5,11,15],[4,9,2],[1,10,5]] 的结果。所以这意味着只有第一个元素将被排序,其余部分相同。
  • public class New1 { public static void main(String[] args) {int x=0,y=0,g=0,cost=1;fun1(x,y,g,cost) ;}public static int[] fun1(int x, int y, int g,int cost) { int [] a=new int[3]; for (int i = 0; i
  • 拜托,单独写一点)))
猜你喜欢
  • 2017-06-06
  • 2013-04-03
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
相关资源
最近更新 更多