简体   繁体   English

从堆栈中删除项目 - Java

[英]Removing an Item from a stack - Java

I have a stack with different colors represented as strings. 我有一个用不同颜色表示为字符串的堆栈。 What I need to do is remove a certain color from the tag. 我需要做的是从标记中删除某种颜色。 I've thought about creating an empty stack, popping the items from the initial stack into this stack except the color and then popping those items again to get the proper order. 我已经考虑过创建一个空堆栈,将项目从初始堆栈弹出到此堆栈(颜色除外),然后再次弹出这些项目以获得正确的顺序。 I'm unsure on how to pop items and place them in a new stack as well as how to remove a certain item. 我不确定如何弹出项目并将其放置在新堆栈中以及如何删除特定项目。 Thanks. 谢谢。 This is the code I have so far. 这是我到目前为止的代码。

import java.util.Collections;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;

class CandiesStack{
//Methods to be used by stack

public static void showStack(Stack<String> candies){
    int num = 1;
    ListIterator<String> it = candies.listIterator(candies.size());
    while(it.hasPrevious()){
        System.out.println("Candy #" + num +" " + it.previous());
        num++;
    }
}

public static void removeYellow(Stack<String> candies){
    //Temporary Stack
    Stack<String> temp = new Stack<String>();       
    ListIterator<String> it = candies.listIterator(candies.size());

    while(it.hasNext()){
        String p = it.next();
            if(p.equals("yellow")){
                it.remove();
            }
    }
}
}

public class PezStack {

public static void main(String[] args) {
    System.out.println("--------------------------------------------------------------------------------------------------------");
    System.out.println("Welcome to PEZ Application using Stack");
    System.out.println("--------------------------------------------------------------------------------------------------------");
    Scanner kb = new Scanner(System.in);
    System.out.println("Here is the content of your PEZ container");
    System.out.println("--------------------------------------------------------------------------------------------------------\n");
    Stack<String> candies = new Stack<String>(); //Initial stack
    //Initiate the candies present in stack
    candies.push("yellow");
    candies.push("green");
    candies.push("pink");
    candies.push("orange");
    candies.push("green");
    candies.push("yellow");
    candies.push("yellow");
    candies.push("pink");
    candies.push("green");
    candies.push("yellow");
    candies.push("pink");
    candies.push("orange");
    Collections.shuffle(candies); //Shuffle content of the stack

    System.out.println("Current Pez Container");
    System.out.println("--------------------------------------------------------------------------------------------------------");

    CandiesStack.showStack(candies);

    System.out.println("Current Pez Container after remove yellow");
    System.out.println("--------------------------------------------------------------------------------------------------------");

    CandiesStack.removeYellow(candies);
    CandiesStack.showStack(candies);
}
}

You don't need a temporary stack at all. 您根本不需要临时堆栈。 The problem you're having is that you're constructing your ListIterator incorrectly in removeYellow() . 您遇到的问题是,你正在构建你ListIterator在正确removeYellow()

When you construct your iterator here: 在这里构造迭代器时:

ListIterator<String> it = candies.listIterator(candies.size());

You're using the ListIterator<E> listIterator(int index) method, which 您正在使用ListIterator<E> listIterator(int index)方法

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. 从列表中的指定位置开始,返回列表中元素的列表迭代器(按正确顺序)。 The specified index indicates the first element that would be returned by an initial call to next. 指定的索引指示初始调用next时将返回的第一个元素。 An initial call to previous would return the element with the specified index minus one. 对previous的初始调用将返回指定索引减去1的元素。

This means that the iterator you've made is starting at the end of the data in your stack, so your first call to it.hasNext() is going to be false . 这意味着你所做的迭代器是从堆栈中数据的末尾开始的,所以你对it.hasNext()第一次调用将是错误的

When you want to use is the public ListIterator<E> listIterator() method, which 当你想使用的是public ListIterator<E> listIterator()方法,其中

Returns a list iterator over the elements in this list (in proper sequence). 返回此列表中元素的列表迭代器(按适当顺序)。

Then you can use your iterator to look through your data and remove all of the "yellow" entries as you already are doing: 然后,您可以使用迭代器查看数据并删除所有正在执行的“黄色”条目:

public static void removeYellow(Stack<String> candies) {
    ListIterator<String> it = candies.listIterator();

    while (it.hasNext()) {
        String p = it.next();
        if (p.equals("yellow")) {
            it.remove();
        }
    }
}

Although I have a hard time to see a reason for using a stack when shuffling it and then accessing it ramdomly something like this should do the trick to remove all _yellow_s: 尽管我很难理解在改组然后随机访问它时使用堆栈的原因,但这样的事情应该可以解决所有_yellow_s的问题:

public static void removeYellow(Stack<String> candies) {
    while(candies.remove("yellow"));
}

I'd advice to use a list (eg ArrayList) to setup the candies you need and building the stack from that list new Stack<String>(list); 我建议使用列表(例如ArrayList)来设置所需的糖果,并从该列表中构建堆栈new Stack<String>(list);

Why not use 为什么不用

stack.remove("yellow")

While it may seem that objects are not the same (two different Strings with the same values), Stack actually uses method equals to compare them. 虽然看起来对象不一样(两个不同的字符串具有相同的值),但Stack实际上使用方法equals来比较它们。 Therefore it is easy to do with strings, and anything else which is easily comparable with equals . 因此,很容易使用字符串,以及其他任何容易与equals相媲美的东西。

EDIT: OK,I get it now. 编辑:好的,我现在明白了。 So you can do it like that: 因此,您可以这样做:

while (stack.remove("yellow"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM