-1

I need a container to keep track of a bunch of objects (public class Note), and a Stack seems to be perfect for what I need. The container's called listOfNotes. But when I try to push a Note onto the stack with:

listofNotes.push(Note0),

the compiler gives me this error:

warning: [unchecked] unchecked call to push(E) as a member of the raw type Stack.

Does this mean stacks can't hold objects, or am I using the command "push" wrong?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
lewis.k
  • 23
  • 1
  • 3
  • Please post a sample of your code illustrating the issue. – CollinD Sep 23 '15 at 20:26
  • You have to define the Type e.g. List means you are defining a type of this list to string. You might want to List or what ever your class is called. – StackFlowed Sep 23 '15 at 20:27
  • 2
    It means the declaration of your stack does not specify what type of objects it can hold. It is anyway a *warning*, not an error. – John Bollinger Sep 23 '15 at 20:28
  • Please post your full corpus of code here, formatted properly (via Ctrl+K). From the sounds of things, you've declared `listOfNotes` as a `Stack` (with no generic type argument) instead of a `Stack`. Please look into [Java Generics](https://en.wikipedia.org/wiki/Generics_in_Java), since all language-provided container classes use generics (since Java 1.5/J2SE 5.0). – Shotgun Ninja Sep 23 '15 at 20:30

3 Answers3

0

You have to define the Type e.g.

List<String> 

means you are defining a type of this list to string. You might want to

List<Node> 

or what ever your class is called.

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

Its difficult to say without a code sample but something like this should work:

 Stack stack = new Stack();
 Note note = new Note();
 stack.push(note);

Can you give a fuller code sample?

justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
0

It's only a warning (not an error) and everything's ok. You may advise the compiler to ignore that kind of warning, for example by using the annotation @SuppressWarnings("rawtypes") in your code. Do so, if it IS ok for you. In that case you may need to cast when popping from the Stack, like that:

Note note = (Note) listOfNotes.pop();

But for better style and usability you should consider to give the container (Stack) the intended type (see 'Generics'):

package yourpackage;
import java.util.Stack;

public class Note {
    public static void main(String[] a) {
        Stack<Note> listOfNotes = new Stack<>();
        listOfNotes.push(new Note());
    }
}

So the code for retrieving Objects from the Stack is like that:

Note note = listOfNotes.pop();
hab
  • 148
  • 1
  • 9