I created a user defined class pair to add two elements in stack. But how can I use peek function of the stack? I want to access the "element" of pair class using peek.
import java.util.*;
public class Example {
public static class pair{
int element;
int idx;
public pair(int element, int idx){
this.element = element;
this.idx = idx;
}
}
public static void main(String[] args) {
Stack<pair> st= new Stack<pair>();
pair p1 = new pair(100,2);
st.push(p1);
System.out.println(st.peek());
}
}
- When I am trying to print st.peek() I got output: Example$pair@4617c264