在同一个类中调用堆栈上的方法

时间:2015-12-22 05:37:07

标签: java data-structures stack

我正在创建一个程序,它接受Integers的序列并将它们放入StatisticianStack。我无法弄清楚如何调用此堆栈上的其他方法。关于如何在nextNumber方法中调用所有其他方法的任何建议?

StatisticianStack:

public class StatisticianStack {

    Stack<Integers> stack = new Stack();


    public  void nextNumber(Integer stackNums){
        stack.push(stackNums);
        stack.length(); // what I would want to do, but do not know what to pass it.

    }

     public static int length(StatisticianStack numbers){

         if(numbers.isEmpty())
                 return 0;

         int sizeOfStack = numbers.size();
         return sizeOfStack;
    }

     public static int sum(Stack<Integer> numbers){
         int sum = 0;
         if(numbers.isEmpty())
                 return sum;

         for(int i = 0; i < numbers.size(); i++)
             sum = sum + numbers.pop();

         return sum;
     }

     public static Double mean(Stack<Integer> numbers){
         double mean = 0;
         double sum = 0;

         if(numbers.isEmpty())
                 return Double.NaN;

         for (int i = 0; i < numbers.size(); i++){
             sum += i;
         }
         mean = sum/numbers.size();
         return mean;
     }

     public static Double largestNum(Stack<Integer> numbers){
         double largestNum = numbers.firstElement();

         if(numbers.isEmpty())
                 return Double.NaN;

         for (int i = 0; i < numbers.size(); i++){
             if(largestNum < numbers.pop())
                 largestNum = numbers.pop();

         }
         return largestNum;
     }

     public static Double smallestNum(Stack<Integer> numbers){
         double smallestNum = numbers.firstElement();

         if(numbers.isEmpty())
                 return Double.NaN;

         for (int i = 0; i < numbers.size(); i++){
             if(smallestNum > numbers.pop())
                 smallestNum = numbers.pop();

         }
         return smallestNum;
     }
}

主要

public class StatisticianStackDemonstartion {

    public static Integer numbers;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    StatisticianStack stack = new StatisticianStack();
    stack.nextNumber(-1);


    }

}

`

任何提示?谢谢。

2 个答案:

答案 0 :(得分:3)

现在我明白了。您可能想在这里想到面向对象

您有一个StatisticianStack类型的对象:

fn permutations_(sequence: Vec<i32>) -> Vec<Vec<i32>> {
    if sequence.len() == 0 {
        let x: Vec<Vec<i32>> = vec![vec![]];
        return x
    }

    let mut result: Vec<Vec<i32>> = Vec::new();

    for (i, item) in permutations_(to_vec(&sequence[0..sequence.len() - 1])).iter().enumerate() {

        let mut n = reverse(&(0..item.len() + 1).collect::<Vec<usize>>());

        if i % 2 != 0 {
            n = (0..item.len() + 1).collect::<Vec<usize>>();
        }

        for k in n {
            let mut items = Vec::new();

            for x in &item[0..k] {
                items.push(*x);
            }

            items.push(item[item.len() - 1]);

            for x in &item[k..item.len()] {
                items.push(*x);
            }

            result.push(items);
        }
    }

    result
}

然后你把一些数字放到那个堆栈中:

StatisticianStack stack = new StatisticianStack();

然后你想对堆栈中的所有元素进行一些计算:

stack.nextNumber(-1);
stack.nextNumber(2.34);
stack.nextNumber(17);

为此,您需要先更改方法签名:

int length = stack.getLength();
double mean = stack.getMean();
double largest = stack.getLargestNumber();
double smallest = stack.getSmallestNumber();

请确认这是你想要的 - 然后我们用生活填充这些方法; - )

您确认 - 这是使用Java 8 Lambda表达式完成的示例:

public class StatisticanStack {
  Stack<Double> stack = new Stack<Double>();

  public void nextNumber(Double stackNums) {
    stack.push(stackNums);
  }

  public int length() {
    return stack.size();
  }

  public double sum() {

  }

  public double mean() {

  }

  public double largestNum() {

  }

  public double smallestNum() {

  }
}

或者如果你更喜欢没有Lambdas的工作:

import java.util.Stack;

public class StatisticanStack {

    Stack<Double> stack = new Stack<Double>();

    public void nextNumber(Double stackNums) {
        stack.push(stackNums);
    }

    public void nextNumber(Integer stackNums) {
        stack.push(new Double(stackNums));
    }

    public int length() {
        return stack.size();
    }

    public double sum() {
        return stack.stream()
        .mapToDouble(Double::doubleValue)
        .sum();
    }

    public double mean() {
        return stack.stream()
                .mapToDouble(Double::doubleValue)
                .average().getAsDouble();
    }

    public double largestNum() {
        return stack.stream()
                .mapToDouble(Double::doubleValue)
                .max().getAsDouble();
    }

    public double smallestNum() {
        return stack.stream()
                .mapToDouble(Double::doubleValue)
                .min().getAsDouble();
    }

    public static void main(String... args) {
        StatisticanStack stack = new StatisticanStack();
        stack.nextNumber(-1);
        stack.nextNumber(2.34);
        stack.nextNumber(17);
        System.out.println(stack.smallestNum());
        System.out.println(stack.largestNum());
        System.out.println(stack.mean());
        System.out.println(stack.sum());
    }

}

等等。

你可能想澄清你想要的“最小”数字:最接近零?最负面的?

答案 1 :(得分:0)

这里有很多psudo代码和缺少的上下文。但您同时拥有stack.push(stackNums)stacks.push(stackNums)。您能提供更多上下文和错误消息吗?