Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6550977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:16:52+00:00 2026-05-25T12:16:52+00:00

import java.util.Iterator; import java.util.Stack; public class StackExample { public static void main(String args[]){ Stack<String>

  • 0
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 before 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:16:52+00:00Added an answer on May 25, 2026 at 12:16 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.*; public class Test { public static void main(String[] args) { Map<String,String> map
import java.util.Scanner; class Practice { public static void main(String args[]) { System.out.println(Enter the number
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
in the following snippet: package test; import java.util.Collection; import java.util.Iterator; import java.util.Map; public class
import java.util.Scanner; public class Test { private static int decimalNum = 0; private static
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.