type List interface{Size()intGet(index int)(interface{},error)Set(index int, newValue interface{})errorInsert(index int, val interface{})errorAppend(val ...interface{})Delete(index int)errorString()stringClear()}
數據結構
type ArrayList struct{
DataStore []interface{}
TheSize int}
新建一個ArrayList
// 新建funcNewArrayList()*ArrayList {// func new(Type) *Type
list :=new(ArrayList)
list.DataStore =make([]interface{},0,10)
list.TheSize =0return list
}
方法實現
// 返回長度func(list *ArrayList)Size()int{return list.TheSize
}// 獲取數據func(list *ArrayList)Get(index int)(interface{},error){if index <0|| index >=len(list.DataStore){returnnil, errors.New("索引越界")}return list.DataStore[index],nil}// 重置元素func(list *ArrayList)Set(index int, newValue interface{})error{if index <0|| index >=len(list.DataStore){return errors.New("索引越界")}
list.DataStore[index]= newValue
returnnil}// 插入元素// 是否空間不足funccheckIsFull(list *ArrayList){iflen(list.DataStore)==cap(list.DataStore){
newDataStore :=make([]interface{},0,2*cap(list.DataStore))copy(newDataStore, list.DataStore)
list.DataStore = newDataStore
}}func(list *ArrayList)Insert(index int, val interface{})error{checkIsFull(list)
list.DataStore =append(list.DataStore,0)
n := list.TheSize
for; n > index; n--{
list.DataStore[n]= list.DataStore[n -1]}
list.DataStore[index]= val
list.TheSize++returnnil}// 追加數據func(list *ArrayList)Append(val...interface{}){
list.DataStore =append(list.DataStore, val...)
list.TheSize+=len(val)}// 刪除元素func(list *ArrayList)Delete(index int)error{//append(slice []Type, elems ...Type) []Type//elems ...Type means that it will accept a variable number of arguments of type Type.//these variadic arguments will be converted to a slice.//So we list.DataStore[index+1:]... ,not list.DataStore[index+1:].
list.DataStore =append(list.DataStore[:index], list.DataStore[index+1:]...)
list.TheSize--returnnil}// 字符打印func(list *ArrayList)String()string{//Sprint(a ...interface{}) string, same as Delete()return fmt.Sprint(list.DataStore...)}// 清空/*
1. To remove all elements, simply set the slice to nil.
a := []string{"A", "B", "C", "D", "E"}
a = nil
fmt.Println(a, len(a), cap(a)) // [] 0 0
2. Keep allocated memory . To keep the underlying array, slice the slice to zero length.
a := []string{"A", "B", "C", "D", "E"}
a = a[:0]
fmt.Println(a, len(a), cap(a)) // [] 0 5
If the slice is extended again, the original data reappears.
fmt.Println(a[:2]) // [A B]
*/func(list *ArrayList)Clear(){// 1
list.DataStore =nil
list.TheSize =0}
整個代碼
main.go
package main
import("fmt""github.com/iceriverdog/dataStruct/ArrayList")funcmain(){
list := ArrayList.NewArrayList()//var list ArrayList.List = ArrayList.NewArrayList()//Append(val ...interface{})
list.Append("abc","abc","3",true)
fmt.Println("Append() 後的 list :",*list)//Size() int
fmt.Println("list 的 size :", list.Size())//Get(index int) (interface{}, error)
val,_:= list.Get(1)
fmt.Println("index 爲 1 的值:", val)//Insert(index int, val interface{}) error
list.Insert(1,"5")
fmt.Println("insert 後:",*list)//Delete(index int) error
list.Delete(4)
fmt.Println("delete 後:",*list)//String() string
s := list.String()
fmt.Println("list.String()", s)// clear(_)
list.Clear()
fmt.Println("clear:", list)}
package ArrayList
import("errors""fmt")// 接口type List interface{Size()intGet(index int)(interface{},error)Set(index int, newValue interface{})errorInsert(index int, val interface{})errorAppend(val ...interface{})Delete(index int)errorString()stringClear()}// 數據結構type ArrayList struct{
DataStore []interface{}
TheSize int}// 新建funcNewArrayList()*ArrayList {// func new(Type) *Type
list :=new(ArrayList)
list.DataStore =make([]interface{},0,10)
list.TheSize =0return list
}// 返回長度func(list *ArrayList)Size()int{return list.TheSize
}// 獲取數據func(list *ArrayList)Get(index int)(interface{},error){if index <0|| index >=len(list.DataStore){returnnil, errors.New("索引越界")}return list.DataStore[index],nil}// 重置元素func(list *ArrayList)Set(index int, newValue interface{})error{if index <0|| index >=len(list.DataStore){return errors.New("索引越界")}
list.DataStore[index]= newValue
returnnil}// 插入元素// 是否空間不足funccheckIsFull(list *ArrayList){iflen(list.DataStore)==cap(list.DataStore){
newDataStore :=make([]interface{},0,2*cap(list.DataStore))copy(newDataStore, list.DataStore)
list.DataStore = newDataStore
}}func(list *ArrayList)Insert(index int, val interface{})error{checkIsFull(list)
list.DataStore =append(list.DataStore,0)
n := list.TheSize
for; n > index; n--{
list.DataStore[n]= list.DataStore[n -1]}
list.DataStore[index]= val
list.TheSize++returnnil}// 追加數據func(list *ArrayList)Append(val...interface{}){
list.DataStore =append(list.DataStore, val...)
list.TheSize+=len(val)}// 刪除元素func(list *ArrayList)Delete(index int)error{//append(slice []Type, elems ...Type) []Type//elems ...Type means that it will accept a variable number of arguments of type Type.//these variadic arguments will be converted to a slice.//So we list.DataStore[index+1:]... ,not list.DataStore[index+1:].
list.DataStore =append(list.DataStore[:index], list.DataStore[index+1:]...)
list.TheSize--returnnil}// 字符打印func(list *ArrayList)String()string{//Sprint(a ...interface{}) string, same as Delete()return fmt.Sprint(list.DataStore...)}// 清空/*
1. To remove all elements, simply set the slice to nil.
a := []string{"A", "B", "C", "D", "E"}
a = nil
fmt.Println(a, len(a), cap(a)) // [] 0 0
2. Keep allocated memory . To keep the underlying array, slice the slice to zero length.
a := []string{"A", "B", "C", "D", "E"}
a = a[:0]
fmt.Println(a, len(a), cap(a)) // [] 0 5
If the slice is extended again, the original data reappears.
fmt.Println(a[:2]) // [A B]
*/func(list *ArrayList)Clear(){// 1
list.DataStore =nil
list.TheSize =0}