【问题标题】:How can I find the index of a value to compare to other elements如何找到值的索引以与其他元素进行比较
【发布时间】:2019-07-03 19:04:36
【问题描述】:

我正在尝试编写一个方法 public static void removeDownTo (StackX stack, long n):它将所有值从堆栈中弹出,但不包括它看到的等于第二个参数的第一个元素。如果没有一个相等,则将堆栈留空。

我试图通过达到 n 值弹出堆栈的上半部分来解决问题。但是堆栈没有排序,因此会导致一些问题。

public class StackX {
   private int maxSize;        // size of stack array
   private long[] stackArray;
   private int top;            // top of stack
//--------------------------------------------------------------
public StackX(int s)         // constructor
  {
  maxSize = s;             // set array size
  stackArray = new long[maxSize];  // create array
  top = -1;                // no items yet
  }
//--------------------------------------------------------------
public void push(long j)    // put item on top of stack
{
  if (!isFull())
      stackArray[++top] = j;     // increment top, insert item
  else
     System.out.println("Can't insert, stack is full");
}
//--------------------------------------------------------------
public long pop()           // take item from top of stack
{
  if(!isEmpty())
     return stackArray[top--];  // access item, decrement top
  else
    System.out.print("Error: Stack is empty. Returning -1");   
   return -1;
}

//--------------------------------------------------------------
public long peek()          // peek at top of stack
{
   if (isEmpty()){
       System.out.print("stack is empty");
   }
  return stackArray[top];
}
//--------------------------------------------------------------
public boolean isEmpty()    // true if stack is empty
{
  return (top == -1);
}
//--------------------
public boolean isFull()     // true if stack is full
{
  return (top == maxSize-1);
}
//--------------------------------------------------------------
public void removeDownTo (StackX stack, long n){ 
   for(int i = 0; stackArray[i] < n; i++){
       stack.pop();
   }
   for(int j = 0; stackArray[j] <= maxSize; j++){
       System.out.println(stackArray[j]);
   } 
  }
}

公共类 StackApp {

public static void main(String[] args) {
    StackX theStack = new StackX(10); // make new stack
    theStack.push(20); // push items onto stack
    theStack.push(40);
    theStack.push(60);
    theStack.push(80);
    while( !theStack.isEmpty()){ // until it's empty,
        theStack.removeDownTo(theStack, 40);
        long value = theStack.pop();
        System.out.print(value); // display it
        System.out.print(" ");
    } // end while
} // end main()

} // 结束类 StackApp

我希望看到 60 80,但结果却是 60 20。

【问题讨论】:

    标签: java


    【解决方案1】:

    由于您是编程新手,因此您的讲师给了您一个很容易解决的任务。你应该严格遵守它的话。这些话给了你主要的线索。

    你应该定义这个方法:

    public static void removeDownTo (StackX stack, long n)
    

    在这里,static 这个词很重要。这意味着该方法不应进入StackX 类。 (说明应该在某处提到这一点。)如果您的任务是将方法添加到 StackX 类,它应该是这样的:

    public void removeDownTo (long n)
    

    这两种方法的区别在于后者可以访问StackX类的所有实现细节,即变量maxSizestackArraytop

    但你的任务不同,你的方法应该是static,这意味着它无权访问这些实现细节。您所能做的就是调用标记为public 的方法。其中有 5 个,它们都以小写字母开头。仅使用这 5 种方法,您应该可以解决这个难题,正如您所写的:

    它将所有值从堆栈中弹出,但不包括它看到的等于第二个参数的第一个元素。如果没有一个相等,则将堆栈留空。

    通过列出上面的 5 种方法,您可以看到堆栈只允许很少的操作。想想一大摞书。你不能只从中间拿一本书,你唯一能做的就是看堆栈的顶部。这就是a stack 的本质。

    你试过了:

    我试图通过达到 n 值弹出堆栈的上半部分来解决问题。但是堆栈没有排序,所以会导致一些问题。

    这个任务比你想象的要简单得多。这根本不是关于排序的。更仔细地遵循说明中的文字。最后,你的 removeDownTo 方法应该是 5 行,从头到尾。这意味着在大括号内,您只需要编写 3 行代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      相关资源
      最近更新 更多