To instantiate the code for a stack generic class using different data types __java

Source: Internet
Author: User
package generic type;
Import Java.math.BigDecimal;

Import Java.math.MathContext;
		public class Stackdemoapp {public static void main (string[] args) {//Long type stack System.out.println ("Create ' long ' type stack");
		Create a stack object that holds a Long data type stack<long> longstack = new stack<long> ();//This stack is the class that you created below that has the Push,pop method
		System.out.println ("Press into 5");
		Longstack.push (5L);//5l also OK, (long) 5 also line System.out.println ("press into 10");
		Longstack.push (10L);
		System.out.println ("Empty stack");
		System.out.println (Longstack.pop ());
		System.out.println (Longstack.pop ());
		
		System.out.println ();
		Float type stack System.out.println ("Create ' float ' type stack");
		stack<float> floatstack = new stack<float> ();
		System.out.println ("Press into 5.0");
		Floatstack.push (5.0f);
		System.out.println ("Press into 10.0");
		Floatstack.push (10.0f);
		System.out.println ("Empty stack");
		System.out.println (Floatstack.pop ());
		System.out.println (Floatstack.pop ());
		
		System.out.println ();
		Large integer type stack System.out.println ("Create ' BigDecimal ' stack"); stack<bigdecimal> BiGdecimalstack = new stack<bigdecimal> ();
		System.out.println ("Press into Bigdecimal:12.5e+7");
		Bigdecimalstack.push (New BigDecimal ("12.5E+7"));
		System.out.println ("Press into bigdecimal:125");
		Bigdecimalstack.push (New BigDecimal (125,mathcontext.decimal128));
		System.out.println ("Empty stack");
		System.out.println (Bigdecimalstack.pop ());
		System.out.println (Bigdecimalstack.pop ());
		
		System.out.println ();
		No generic stack stack oldtypestack = new stack ();
		Oldtypestack.push (10);
		Oldtypestack.push ("test string");
			for (int i=0;i<2;i++) {string str = (string) oldtypestack.pop ();
		System.out.println (str);
	"}}//Custom stack class Stack<t> {//private t[] stack =new t[5];//wrong wrong .....
	Private t[] stack = (t[]) New object[5];//t can be String,integer//For example: string[] stack= (string[)) new object[5];
	Float[] stack= (float[]) new object[5];
	private int ptr =-1;
		void push (T data)//indentation method push {ptr++;
		
	Stack[ptr]=data;
	T pop ()//Eject method {return (T) stack[ptr--];
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.