1

I have below two classes

import java.util.*;
public interface Stack<Item> extends Iterable<Item>{
    void push(Item item);
    Item pop();
    boolean isEmpty();
    int size();
    Iterator<Item> iterator();
}

Second class is the :

import java.util.*;
public class LinkedStack<Item> implements Stack<Item>{

private Node head;
private int size;


private class Node{
    Item item;
    Node next;

    public Node(Item item){
        this.item = item;

    }

    public Node(Item item, Node next){
        this.item = item;
        this.next = next;

    }
}


public boolean isEmpty(){
    return(head == null);
}

public int size(){
    return size;
}

public void push(Item item){
    head = new Node(item,head);
    size++;
}   

public Item pop(){
    Item item = head.item;
    head = head.next;
    size--;
    return item;
}

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

class LinkedStackIterator<Item> implements Iterator<Item>{
    private Node current = head;

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

    public Item next(){
        Item return_item = current.item;
        current = current.next;
        return return_item;
    }

    public void remove(){};
}
}

I am getting typecast error in method public Item next():

Item return_item = current.item;

If I write above line as

Item return_item = (Item) current.item;

It works fine. Can anybody suggest the reason?

I am getting below compilation error:

LinkedStack.java:57: error: incompatible types Item return_item = current.item; ^ required: Item#2 found: Item#1 where Item#1,Item#2 are type-variables: Item#1 extends Object declared in class LinkedStack Item#2 extends Object declared in class LinkedStack.LinkedStackIterator 1 error

4
  • What's the type of current.item? Commented Jun 25, 2014 at 8:44
  • @Christian I suppose it's of type Item. Commented Jun 25, 2014 at 8:45
  • If it were of type Item you won't need to cast it. Look at that class or its documentation to see what type it returns. Check also if Item implements any interface. There may be the answer. Commented Jun 25, 2014 at 8:48
  • They're different Item types. You need to either make Node generic or make LinkedStackIterator not generic. My vote is to make Node generic (and also make it static). Commented Jun 25, 2014 at 8:52

1 Answer 1

1

Because your LinkedStackIterator class is generic, when it refers to Item it means a different type to the Item used in your outer classes. One solution is to change LinkedStackIterator so it is not generic, i.e. change:

class LinkedStackIterator<Item> implements Iterator<Item>{

to

class LinkedStackIterator implements Iterator<Item>{

Side note: it can be quite confusing to Java programmers to see generic type variables given meaningful names like Item. You may wish to consider renaming this to something more expected like T.

Sign up to request clarification or add additional context in comments.

2 Comments

I will follow the convention as suggested by you. Thanks :).
If it's right, mark as correct answer bro @pradeep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.