模拟java中的arraylist
实现arrayList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package arrayList import ( "errors" "fmt" ) type List interface { Append(value... interface{})(error) Remove(index int)(interface{}, error) } // golang的结构体中不可能有默认值 type ArrayList struct { size int elementData []interface{} } func NewArrayList() *ArrayList { list := new(ArrayList) list.size = 0; list.elementData = make([]interface{}, 0, 1); return list } func (list *ArrayList)Get(index int) (interface{}, error) { // 先检查是不是超出范围 if index < 0 || index >= list.size { return nil, errors.New("超出索引范围") } return list.elementData[index], nil } func (list *ArrayList)Set(index int, value interface{}) ( error) { // 先检查是不是超出范围 if index < 0 || index >= list.size { return errors.New("超出索引范围") } list.elementData[index] = value; return nil } func (list *ArrayList)Clear() { list.elementData = make([]interface{}, 0) list.size = 0; } func (list *ArrayList)ToString() string { return fmt.Sprint(list) } func (list *ArrayList) Append(value ...interface{}) error { // 他会自动扩容 for _, v := range value{ list.elementData = append(list.elementData, v) list.size++; } return nil } func (List *ArrayList) Remove(index int) (interface{}, error){ // 先检查是不是超出范围 if index < 0 || index >= List.size { return nil, errors.New("超出索引范围") } oldValue := List.elementData[index]; //怎么赋值一个数组 List.elementData = append(List.elementData[:index], List.elementData[index+1:]...) List.size--; return oldValue, nil } func (List *ArrayList) Insert(index int, value interface{}) (error){ // 先检查是不是超出范围 if index < 0 || index > List.size { return errors.New("超出索引范围") } // 将所有元素后移动一位 List.elementData = append(List.elementData, value) List.size++; for i := List.size - 1; i > index ; i-- { List.elementData[i] = List.elementData[i-1]; } List.elementData[index] = value return nil } |
实现数组迭代器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package arrayList import ( "errors" ) type Iterator interface { HashNext() bool Next() (interface{}, error) Remove() (error) } type Iterable interface { Iterator() Iterator } type ArraylistIterator struct { list *ArrayList cursor int lastRet int; } func (a *ArraylistIterator) Remove() error { if a.lastRet == -1 { return errors.New("不允许这样使用") } _, err := a.list.Remove(a.lastRet) if err != nil { return err } a.cursor = a.lastRet a.lastRet = -1; return nil } func (a *ArraylistIterator) HashNext() bool { return a.cursor != a.list.size } func (a *ArraylistIterator) Next() (interface{}, error) { i := a.cursor if i >= a.list.size { return nil, errors.New("没有这样的索引") } a.cursor = a.cursor + 1; a.lastRet = i return a.list.elementData[a.lastRet], nil; } func (List *ArrayList)Iterator() Iterator { it := new(ArraylistIterator) it.list = List it.cursor = 0 it.lastRet = -1; return it } |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package main import ( "aa/arrayList" "fmt" ) func main() { var list arrayList.List = arrayList.NewArrayList(); list.Append(1) var newArrayList *arrayList.ArrayList = arrayList.NewArrayList() for i := 0; i < 10 ; i++ { newArrayList.Append(1) newArrayList.Append(2) newArrayList.Append(3); newArrayList.Append(4); } fmt.Println(newArrayList) newArrayList.Set(0, 100) fmt.Println(newArrayList.ToString()) newArrayList.Insert(0, 0); fmt.Println(newArrayList.ToString()) newArrayList.Insert(41, 41); fmt.Println(newArrayList.ToString()) newArrayList.Insert(1, 111); fmt.Println(newArrayList.ToString()) it := newArrayList.Iterator() for it.HashNext() { it.Next() it.Remove() } } |