开发者

Few Stack related questions in Java

import java.util.Iterator;
import java.util.Stack;

public class StackExample {

    public static void main(String args[]){

        Stack<String> sk = new Stack<String>();

        sk.push("Hello");
        sk.push("Hello1");
        sk.push("Hello2");
        sk.push("Hello3");

        System.out.println("The Values of Stack" +sk);

        Iterator it=sk.iterator();

        System.out.println("Size be开发者_开发技巧fore pop() :"+sk.size());

        while(it.hasNext())
        {
            String iValue=(String)it.next();
            System.out.println("Iterator value :"+iValue);
        }

        String value =(String)sk.pop();

        System.out.println("value :"+value);

        System.out.println("Size After pop() :"+sk.size());



    }

}

Can anyone explain me the below questions.

while(it.hasNext()){
            String iValue=(String)it.next();
            System.out.println("Iterator value :"+iValue);
}
  1. Why do we iterate in this manner and what does the hasNext and next do? Can i not do the same with an for loop.

    String value =(String)sk.pop();
    
  2. What does the (String)sk.pop means... why does it not compile when i remove the (String)

  3. Can anyone lead me to some good and complex Stack examples in Java


Ad 1.: This is a standard Java Iterator. And since Stack implementans Iterable you can simply write:

for (String item : sk)
{
    System.out.println(item);
}

Ad 2.: Casting to String is required because Iterator is declared without generic type (which is a bad practice). Try with:

Iterator<String> it=sk.iterator();

Casting is no longer needed.

Ad 3.: Here is a simple example written in 10 minutes:

public static void validateXml(String xml) {
    Stack<String> tags = new Stack<String>();
    for (String tag : xml.split("\\s"))
        if(tag.startsWith("</")) {
            if (tags.isEmpty())
                throw new IllegalArgumentException("No start tag matching: " + tag);
            final String startTag = tags.pop();
            if (!startTag.substring(1).equals(tag.substring(2)))
                throw new IllegalArgumentException("Start tag: " + startTag + " does not match end tag: " + tag);
        } else
            tags.push(tag);
    if (!tags.isEmpty())
        throw new IllegalArgumentException("No end tag matching: " + tags);
}

Usage:

validateXml("<a> <b> </b> <c> <d> </d> <e> </e> </c> </a>");
validateXml("<a> <b> </b> <c> <d> <e> </d> </e> </c> </a>");    //fails to validate


1. Why do we iterate in this manner and what does the hasNext and next do? Can i not do the same with an for loop.

The next fetches the next value from the iterator, and hasNext tells you whether or not there is a next value available.

Yes, you could do the same with a for loop:

for (String str: sk)
    System.out.println(str);

should do just as fine for instance.

2. What does the (String)sk.pop means... why does it not compile when i remove the (String)

When you pop an element of a stack, you remove the "top" (last inserted) element. The (String) part is a cast. This is neccesary if the pop returns an Object and (you know it's actually a String) and you want to store it in a String reference.

3. Can anyone lead me to some good and complex Stack examples in Java

Complex stack examples? Uhm, no, I can't. A stack basically provide push and pop which are really simple. Whatever the example would be, it would be complex due to something else than the use of a stack, which won't help you understand stacks any better :-)


Stack is a "legacy" collection, it extends from Vector. Both are there from the old times of Java 1.0. It is not a good idea to use because Vector methods are synchronized. Instead you can use the newest Deque interface, for instance, ArrayDeque or LinkedList.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜