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

  • SEARCH
  • Home
  • 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 6568611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:30:01+00:00 2026-05-25T14:30:01+00:00

Stack<String> sk = new Stack<String>(); sk.push(Hello); sk.push(Hello1); sk.push(Hello2); There are two ways i am

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

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

There are two ways i am iterating this Stack Object.

for(String s : sk){
   System.out.println("The Values of String in SK" +sk);
}

// Way two..

Iterator<String> it=sk.iterator();
    while(it.hasNext())
    {
        String iValue=(String)it.next();
        System.out.println("Iterator value :"+iValue);
    }
  1. What is the difference between these two?
  2. Any Advantage if i choose one among them?
  3. Which is the preferred way of iterating?
  • 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-25T14:30:02+00:00Added an answer on May 25, 2026 at 2:30 pm

    1. What is the difference between these two?

    Not much. The for-each loop construct actually relies on the iterator behind the curtains.

    Further reading:

    • The For-Each Loop, official documentation / tutorial

    2. Any Advantage if i choose one among them?

    Mostly readability I would assume.

    (If you need to access the iterator.remove() method, then obviously you would need to go with the explicit Iterator approach.) However, keep in mind that it’s an optional operation and may not be supported by the underlying Stack implementation you’re using.

    Besides, the point of a Stack structure is that you don’t remove elements in the middle.

    3. Which is the preferred way of iterating?

    Use the for-each approach, if that works for you.

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

Sidebar

Related Questions

How does a stack overflow occur and what are the ways to make sure
Which technology stack is best for the creation of a two dimensional MMORPG and
I'm not very good at C, and I always get stuck on simple string
Stack Overflow has a subversion version number at the bottom: svn revision: 679 I
I see in the Stack Overflow footer that the SVN Revision number is displayed.
An answer to a Stack Overflow question stated that a particular framework violated a
What are the stack and heap? Where are they located physically in a computer's
Here on Stack Overflow, you're a user. On 43things.com you're a person. On other
I have read on Stack Overflow some people that have converting to C#2.0 to
Posting a stack overflow question on stackoverflow.com, how amusing :-) I'm running some recursive

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.