Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BT-ICD/83535f1e8e9adf8ac561d40045c65a5b to your computer and use it in GitHub Desktop.
Save BT-ICD/83535f1e8e9adf8ac561d40045c65a5b to your computer and use it in GitHub Desktop.
Generic Stack - Learn about generic with Array
package GenericDemo;
public class GenericDemoArrayIssue {
public static void main(String[] args) {
ColoredPoint[] cptArray = new ColoredPoint[1];
// cptArray[0]= new Point(); //This is illegal because Point instances have fewer members (only x and y) than ColoredPoint instances (x, y and color). Attempting to access a Point instance's nonexistent color field from its entry in the ColoredPoint array would result in a memory violation (because not memory has been assigned to color) and ultimately crash the JVM.
Point[] ptArray = cptArray; //This is legal because of covariance (an array of supertype references is a supertype of an array of subtype references). In this case, an array of point references is a supertype of an array of ColoredPoint references. Covariance is dangerous when abused. For Example line (ptArray[0] = new Point();) results in ArrayStoredException at runtime because a Point instance is not a ColoredPoint instance. Without this exception, an attempt to access the nonexistent member color crashes the JVM.
// ptArray[0]= new Point(); //Exception in thread "main" java.lang.ArrayStoreException: GenericDemo.Point
ptArray[0]= new ColoredPoint(); //No error
System.out.println(ptArray[0].toString());
}
}
class Point{
int x,y;
}
class ColoredPoint extends Point{
int color;
}
/**
* Example: Generic Method with upper bound
* The type parameter section specifies that T extends Comparable< T >only objects of classes that implement interface Comparable< T > can be used with this method.
* In this case, Comparable is known as the upper bound of the type parameter.
* By default, Object is the upper bound.
* */
package GenericDemo;
public class GenericMethodUpperBoundDemo1 {
public static void main(String[] args) {
int ans;
ans = maximum(40,125,60);
System.out.println("Maximum is " + ans);
}
public static <T extends Comparable<T> > T maximum(T a, T b , T c){
T max =a;
if(b.compareTo(max)>0){
max=b;
}
if(c.compareTo(max)>0){
max =c;
}
return max;
}
}
package GenericDemo;
public class GenericStack<T> {
private int top=-1, size=5;
// private T[] data = new T[size];
private T[] data;
GenericStack(){
data = (T[]) new Object[size];
}
void push(T value){
if(top<size-1){
top++;
data[top]=value;
}
else {
System.out.println("Overflow ");
}
}
T pop(){
if(top>-1){
T result = data[top];
top--;
return result;
}
System.out.println("Underflow");
return null;
}
}
package GenericDemo;
public class GenericStackDemo {
public static void main(String[] args) {
// myStackDemo();
// genericStackIntegerDemo();
genericStackSquareDemo();
}
private static void genericStackSquareDemo() {
GenericStack<Square> stack = new GenericStack<>();
stack.push(new Square(10));
stack.push(new Square(20));
stack.push(new Square(30));
stack.push(new Square(40));
stack.push(new Square(50));
stack.push(new Square(60));
System.out.println(stack.pop());
}
private static void genericStackIntegerDemo() {
GenericStack<Integer> stack = new GenericStack<>();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
// System.out.println(stack.pop());
// System.out.println(stack.pop());
// System.out.println(stack.pop());
// System.out.println(stack.pop());
// System.out.println(stack.pop());
// System.out.println(stack.pop());
// stack.push(101);
// System.out.println(stack.pop());
// System.out.println(stack.pop());
}
private static void myStackDemo() {
MyStack stack = new MyStack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.push(101);
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
package GenericDemo;
public class MyStack {
private int top=-1, size=5;
private int[] data = new int[size];
void push(int value){
if(top<size-1){
top++;
data[top]=value;
}
else {
System.out.println("Overflow ");
}
}
int pop(){
if(top>-1){
int result = data[top];
top--;
return result;
}
System.out.println("Underflow");
return -1;
}
}
package GenericDemo;
public class Square {
private int length;
public Square(int length) {
this.length = length;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
@Override
public String toString() {
return "Square{" +
"length=" + length +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment