一、數據結構,單鏈表的實現(這裏實現了增、刪、查功能)
public class Node {
private String content;
private Node next; //用以指向下一節點
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class MyList {
//root這個節點是頭節點,只有我能操作,不向外公開
private final Node root = new Node(); //這個節點沒有保存任何內容,沒有指向任何一個節點,但並不代表它不存在。把他當作頭節點
private int size = 0;
//添加節點,改變鏈表的結構
public void add(Node node) {
Node temp = root;//注意根節點一定不能動,要是動了馬上就找不回來了(所以我上面把root設置爲private final的,防止被修改),所以用temp來動
while (temp.getNext()!=null) { //只要temp的下一個不爲空,就一直向後遍歷
temp = temp.getNext();
}
temp.setNext(node);
size++;
}
//add方法的重載,這個方法便於添加節點,不用想上面那個方法,每次添加節點都需要先創建
public void add(String content) {
Node node = new Node();
node.setContent(content);
this.add(node);
}
//獲取節點,不改變節點的結構
public Node get(int index) {
Node result = root;//臨時節點,用以遍歷
if(index < 0 || index > size) {
System.out.println("下標有誤!");
}
for(int i = 0; i < index+1; i++) {
result = result.getNext();
}
return result;
}
//刪除尾節點,並返回值
public Node remove() {
Node temp = root;
Node tail = temp.getNext();
if(tail == null) {//先判斷鏈表是否爲空,如果爲空直接返回,不需要再做後續的size--工作了
return = null;
}
whlie(tail.getNext() != null) {
temp = tail; //1.temp後移
tail = tail.getNext();//2.tail後移,注意1和2的順序不能能變
}
temp.setNext(null);
size--;
return tail;
}
public int size() {
return size;
}
}
這時我們發現,自己做的這個容器比數組好在可以任意變長,劣勢是這裏只能保存String類型的數據。如果要存其他類型數據,就需要改Node類裏面的content的類型。很麻煩,所以這時就引入了泛型。
二、泛型(部分)
這裏先對泛型做以瞭解,方便理解馬上要學習的容器
泛型:用來對類型進行約束,並且進行統一化管理。具體的這個類的類型不知道,創建對象傳什麼類型他就是什麼類型
泛型名一般爲:E(element)、T(type)、K (key)、V(value)
普通泛型:
class Point<T>{ // 此處可以隨便寫標識符號,T是type的簡稱
private T var ; // var的類型由T指定,即:由外部指定
public T getVar(){ // 返回值的類型由外部決定
return var ;
}
public void setVar(T var){ // 設置的類型也由外部決定
this.var = var ;
}
};
public class GenericsDemo06{
public static void main(String args[]){
Point<String> p = new Point<String>() ; // 裏面的var類型爲String類型
p.setVar("it") ; // 設置字符串
System.out.println(p.getVar().length()) ; // 取得字符串的長度
}
};
----------------------------------------------------------
class Notepad<K,V>{ // 此處指定了兩個泛型類型
private K key ; // 此變量的類型由外部決定
private V value ; // 此變量的類型由外部決定
public K getKey(){
return this.key ;
}
public V getValue(){
return this.value ;
}
public void setKey(K key){
this.key = key ;
}
public void setValue(V value){
this.value = value ;
}
};
public class GenericsDemo09{
public static void main(String args[]){
Notepad<String,Integer> t = null ; // 定義兩個泛型類型的對象
t = new Notepad<String,Integer>() ; // 裏面的key爲String,value爲Integer
t.setKey("湯姆") ; // 設置第一個內容
t.setValue(20) ; // 設置第二個內容
System.out.print("姓名;" + t.getKey()) ; // 取得信息
System.out.print(",年齡;" + t.getValue()) ; // 取得信息
}
};
通配符
class Info<T>{
private T var ; // 定義泛型變量
public void setVar(T var){
this.var = var ;
}
public T getVar(){
return this.var ;
}
public String toString(){ // 直接打印
return this.var.toString() ;
}
};
public class GenericsDemo14{
public static void main(String args[]){
Info<String> i = new Info<String>() ; // 使用String爲泛型類型
i.setVar("it") ; // 設置內容
fun(i) ;
}
public static void fun(Info<?> temp){ // 可以接收任意的泛型對象
System.out.println("內容:" + temp) ;
}
};
泛型學習引自:https://www.cnblogs.com/sunwei2012/archive/2010/10/08/1845938.html