why it throws java.lang.classCastException
Posted
by matin1234
on Stack Overflow
See other posts from Stack Overflow
or by matin1234
Published on 2010-06-13T08:31:18Z
Indexed on
2010/06/13
8:32 UTC
Read the original article
Hit count: 382
Hi this is my class and I want to sort my stack but it will throw an exception please help me thanks!
public class jj {
public static void main(String[] args){
Stack<Integer> s = new ImplimentingAStackUsingAnArrayOfAGivenSizeN(5);
s.push(1);
s.push(3);
s.push(5);
s.push(2);
s.push(4);
Collections.sort((List<Integer>) (s));
System.out.println(s);
while (!s.isEmpty()) {
System.out.println(s.pop());
}
}
}
the stack traces:
run:
Exception in thread "main" java.lang.ClassCastException: datastructurechapter5.ImplimentingAStackUsingAnArrayOfAGivenSizeN cannot be cast to java.util.List
at datastructurechapter5.jj.main(jj.java:24) `Collections.sort((List<Integer>) (s));`
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
© Stack Overflow or respective owner