arraylist的地層是用陣列來進行搭建的,所以我們可以先用陣列來搭建底層,我們需要乙個變數來計算陣列的大小
然後在乙個乙個的往裡面新增自己需要的方法
//為了定義預設的陣列大小
private
static
final
int default_size=
10;
//一種是有參構造器,另一種是無參構造器,在呼叫的時候會自動的建立oject的陣列(暫時定義object的)
public
myconlection01()
public myconlection01 (
int length_array)
//新增乙個記錄陣列大小的元素
private
int size;
public
void
add(object str)
@override
public string tostring()
sb.setcharat
(sb.
length()
-1,']');
return sb.
tostring()
;}
public
class
myconlection01
public
void
setchart
(int index, object ob)
//列印字串
@override
public string tostring()
sb.setcharat
(sb.
length()
-1,']');
return sb.
tostring()
;}public
myconlection01()
public myconlection01 (
int length_array)
}
public
class
myconlection02
public
void
printlength()
public
void
setchart
(int index, e ob)
@override
public string tostring()
sb.setcharat
(sb.
length()
-1,']');
return sb.
tostring()
;}public
myconlection02()
public myconlection02 (
int length_array)
}
一般是擴容為原來的1.5倍,(size+(size>>1))
public
class
myconlection03
public myconlection03 (
int length_array)
public
void
add(e str)
element[size++
]=str;
}public
void
printlength()
public
void
printsize()
@override
public string tostring()
sb.setcharat
(sb.
length()
-1,']');
return sb.
tostring()
;}}
public
class
myconlection04
public myconlection04 (
int length_array)
else
if(length_array ==0)
else
if(length_array>0)
}public
void
add(e str)
element[size++
]=str;
}public
void
add(e str,
int index)
if(size == element.length||index+
1>= element.length)
trycatch
(exception e)
finally
element[index]
=str;
}public
void
printlength()
public
void
printsize()
@override
public string tostring()
sb.setcharat
(sb.
length()
-1,']');
return sb.
tostring()
;//
}}
public
class
myconlecction05
/***輸入容器的大小
* @param length_array
*/public myconlecction05 (
int length_array)
else
if(length_array ==0)
else
if(length_array>0)
}/**
* 新增乙個內容,在底部
* @param str
*/public
void
add(e str)
element[size++
]=str;
}/**
* 提取某個未知的引數
* @param index
* @return
*/public object getelement
(int index)
return element[index];}
/** * 去除某個位置的引數
* @param index
*/public
void
remove
(int index)
/** * 判斷是否為空容器
* @return
*/public boolean isempty()
/** * 根據位置去增加內容
* @param str
* @param index
*/public
void
add(e str,
int index)
if(size == element.length||index+
1>= element.length)
trycatch
(exception e)
finally
element[index]
=str;
}/**
* 返回容器長度
* @return
*/public
intlength()
/** * 返回size
* @return
*/public
intsize()
@override
public string tostring()
sb.setcharat
(sb.
length()
-1,']');
return sb.
tostring()
;//
}}
github 手寫ArrayList集合
最近仔細的研究了下集合框架的原始碼,並完全手寫下來,這裡將每一步的進展記錄下來,歡迎指點。arraylist的構造器,分別為無參構造器 傳入初始大小的有參構造器 傳入乙個集合型別的有參構造器,這裡只介紹前面兩種常用的構造器。無參構造器 arraylist的底層實際就是使用陣列來實現的,我們建立乙個陣...
手寫ArrayList入門
arraylist是集合的一種實現,實現了介面list,list介面繼承了collection介面。collection是所有集合類的父類。arraylist使用非常廣泛,不論是資料庫表查詢,excel匯入解析,還是 資料爬取都需要使用到,了解arraylist原理及使用方法顯得非常重要。那麼arr...
容器 ArrayList學習
陣列也是一種容器,但是容器更加靈活 更加強大,它的容量是隨時可擴的。set list繼承collection 實現set介面 hashset 實現list介面 arraylist linkedlist 實現map介面 hashmap。list是有序 可重複的容器 list介面的實現類有 arrayl...