Java数据结构-基于数组的栈和泛型
生活随笔
收集整理的這篇文章主要介紹了
Java数据结构-基于数组的栈和泛型
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
泛型可以參數(shù)化變量的類型
當(dāng)你需要用不同類型的基本類型變量來(lái)調(diào)用構(gòu)造方法時(shí)你需要泛型
public class Test {public static void main(String[] args) {A a=new A("a");System.out.println(a.e);A a1=new A(10);System.out.println(a1.e);} } class A<E>{E e;public A(E e){this.e=e;}public E getE(){return e;}}輸出結(jié)果:
a 10如果不滿意 你可以使用兩個(gè)泛型 你甚至可以使用一個(gè)數(shù)組或者自定義的類
/*** Created by root on 16-2-23.*/ public class Test {public static void main(String[] args) {A a=new A("a");System.out.println(a.e);a.setT(new int[]{1,2,3});//你可以在這里賦給t一個(gè)數(shù)組或者以個(gè)自定義類A a1=new A();a1.setT(new B());//賦給a2的成員t一個(gè)B類對(duì)象} } class A<E,T>{E e;T t;public A(){}public A(E e){this.e=e;}public E getE(){return e;}public void setT(T t){this.t=t;}public T getT(){return t;}} class B{int b=10; }debug結(jié)果
有時(shí)你需要對(duì)泛型作出一些約束以免發(fā)生錯(cuò)誤溢出
public class Test {public static void main(String[] args) {A<String,String> a=new A();//對(duì)泛型作出一些約束 讓它只能接受String類型A a2=new A<String,String>();//同樣的約束 但是要約束的參數(shù)要緊跟類名a.setT("a");//正確的方法//a.setT(10); 出現(xiàn)報(bào)錯(cuò)} } class A<E,T>{E e;T t;public A(){}public A(E e){this.e=e;}public E getE(){return e;}public void setT(T t){this.t=t;}public T getT(){return t;}} class B{int b=10; }雖然java不能直接聲明泛型數(shù)組 但是可以利用泛型自己寫一個(gè)泛型數(shù)組類
public class ArrayType<T> {private int index;private int length;T[] data;private T el;public ArrayType(int length){//根據(jù)長(zhǎng)度初始化數(shù)組this.length=length;data=(T[])(new Object[length]);}public void setEl(int thatIndex,Object el){//根據(jù)索引賦值 變量的類型可以使任意的if(thatIndex<length){data[thatIndex]=(T)el;}}public T getEl(int thatIndex){//根據(jù)索引獲取元素的值if(thatIndex<length){return data[thatIndex];}else {return data[0];}} }test class
public class Test {public static void main(String[] args){ArrayType a=new ArrayType(3);a.setEl(0,23);a.setEl(1,"dd");a.setEl(2,true);} }debug結(jié)果:
加入一些變長(zhǎng)的功能
public class ArrayType<T> {private int index;private int length;T[] data;private T el;public ArrayType(int length){this.length=length;data=(T[])(new Object[length]);}public void setEl(int thatIndex,Object el){if(thatIndex<length){data[thatIndex]=(T)el;}else {stretch(thatIndex+1);//如果數(shù)組越界 執(zhí)行復(fù)制data[thatIndex]=(T)el;}}public T getEl(int thatIndex){if(thatIndex<length){return data[thatIndex];}else {stretch(thatIndex+1);//如果數(shù)組越界 執(zhí)行賦值 并返回nullreturn data[thatIndex];}}protected void stretch(int thatLength){T[] newdata=(T[])(new Object[thatLength]);//定義一個(gè)thatLength的數(shù)組for(int i=0;i<this.length;i++){ //把data內(nèi)所有元素復(fù)制到data里newdata[i]=data[i]; //}data=newdata;} }有了以上基礎(chǔ) 可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)組棧
public interface Stack<E> {public boolean isEmpty();public E peek();public E pop();public void push(E target); } import java.util.EmptyStackException; import java.util.Objects;/*** Created by root on 16-2-24.*/ public class ArrayStack<E> implements Stack {//實(shí)現(xiàn)棧的interfaceprivate E[] data; //可以獲得數(shù)組長(zhǎng)度private int size; //用于判斷棧頂?shù)乃饕齪ublic ArrayStack(){data=(E[])(new Object[1]);//初始化的棧具有一個(gè)null類型 size的值為0 size-1是棧頂?shù)乃饕齭ize=0; }public boolean isEmpty(){return size==0; //判斷棧是否為空 }public Object pop(){if (isEmpty()){throw new EmptyStackException();//如果棧為空 那就報(bào)錯(cuò)}size--; //如果棧不為空 return data[size]; //彈出棧頂}public Object peek(){if(isEmpty()){throw new EmptyStackException();//如果棧為空 報(bào)錯(cuò)}return data[size-1];//返回棧頂}protected boolean isFull(){//棧滿了?return size ==data.length;}public void push(Object target){if (isFull()){//如果棧滿了 執(zhí)行擴(kuò)棧stretch();}data[size]= (E) target;//如果棧沒(méi)有滿 把target壓入棧頂size++;}protected void stretch(){//將原數(shù)組的長(zhǎng)度擴(kuò)大兩倍 擴(kuò)大棧的長(zhǎng)度E[] newData=(E[])(new Object[data.length*2]);for(int i=0;i<data.length;i++){newData[i]=data[i];}data=newData;} }轉(zhuǎn)載于:https://www.cnblogs.com/Salaku/p/5208728.html
總結(jié)
以上是生活随笔為你收集整理的Java数据结构-基于数组的栈和泛型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iOS9怎么分屏? iOS9画中画怎么用
- 下一篇: iPhone怎么辨别翻新机?iPhone