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 8045787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:38:34+00:00 2026-06-05T05:38:34+00:00

Can anyone explain why I am getting this error? Here is a stack class

  • 0

Can anyone explain why I am getting this error?

Here is a stack class I implemented using a doubly linked list:

import java.util.Iterator;

public class Stack<Item> implements Iterable<Item>{

private Node first;
private int N;

private class Node{
    private Node next;
    private Node prev;
    private Item item;
}

public Iterator<Item> iterator(){
    return new ReverseIterator<Item>();
}    

private class ReverseIterator<Item> implements Iterator<Item>{
    private Node x;

    private ReverseIterator(){
        if (first != null)
           x = first.prev;
    }

    public boolean hasNext(){
        return x != null;
    }

    public Item next(){
        Item i = x.item;
        x = x.prev;
        return i;
    }

    public void remove(){
    }
}

public void push(Item i){
    if (isEmpty()){
        first = new Node();
        first.item = i;
        first.next = first;
        first.prev = first;
    }
    else{
        Node x = new Node();
        x.item = i;
        x.next = first;
        x.prev = first.prev;
        first.prev.next = x;
        first.prev = x;
    }
    N++;
}

public Item pop(){
    assert !isEmpty() : "Stack is empty";

    Item i = first.prev.item;
    if (N == 1)
        first = null;
    else{
        first.prev.prev.next = first;
        first.prev = first.prev.prev;
    }

    N--;    
    return i;
}

public boolean isEmpty(){
    return N == 0;
}

public int size(){
    return N;
}

public static void main(String[] args){

}
}

The compiler says there’s an error in Item i = x.item;, expected Item, found Item. The solution was to replace ReverseIterator<Item> with ReverseIterator. Can someone explain why I got the error I did by adding <Item>?

Thanks

  • 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-06-05T05:38:35+00:00Added an answer on June 5, 2026 at 5:38 am

    Just because you used the same name for the type variable (Item) does not mean that it represents the same generic type.

    If you declare a nested class N<T>, inside a generic class C<T>, the T from C<T> is effectively hidden from the body of N<T>. It is exactly the same principle as declaring a class level field called x and declaring a method parameter in that class, also called x. Your innermost declaring scope hides anything from the outside.

    If ReverseIterator were a static nested class, you would be obliged to add the <Item> to its declaration, because instances of it would not have an enclosing instance of Stack<Item>. And the same error would result, even though in this case there would be no hiding going on. In fact, you would need to add the type variable to Node as well.

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

Sidebar

Related Questions

Can anyone explain why I'm getting this compile error? Duplicate 'Rad.Core.Aop.MethodArgumentValidation' attribute E:\Scripting\Rad.Core\Properties\AssemblyInfo.cs This
Can anyone explain why isn't the operator[] implemented for a std::list? I've searched around
My first time using triggers. Can anyone please explain why this trigger won't work?
Can anyone explain why isn't this possible (at least in .Net 2.0): public class
can anyone explain why I am getting the following results? Dim badDecimal As Decimal
I am getting this error while creating a public method on a class for
Can anyone explain why this happens? mybox:$ ruby script/console Loading development environment (Rails 2.3.5)
I´m getting this error using NSLock which I tried to circumvent by using unlockWithCondition
Can anyone explain why this works: use MyDb1 if NOT EXISTS (select * from
Could anyone please explain this to me with an example? I am getting contradicted

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.