数组
- 数组的标准定义是:
一个存储元素的线性集合(collection),元素可以通过索引来任意存
取,索引通常是数字,用来计算元素之间存储位置的偏移量。
效率
构建动态数组
package ArrayList
import (
"errors"
"fmt"
)
type List interface {
Size() int
Get(index int)(interface{}, error)
Set(index int, newVal interface{})error
Insert(index int, newVal interface{})error
Append(newVal interface{})
Clear()
Delete(index int)error
String()string
}
type ArrayList struct {
dataStore [] interface{}
TheSize int
}
func NewArrayList()* ArrayList{
list := new(ArrayList)
list.dataStore = make([]interface{}, 0, 10)
list.TheSize = 0
return list
}
func (list *ArrayList)checkIsFull() {
if list.TheSize == cap(list.dataStore) {
newDateStore := make([]interface{}, list.TheSize,2*list.TheSize)
copy(newDateStore, list.dataStore)
list.dataStore = newDateStore
}
}
func (list *ArrayList)Size() int {
return list.TheSize
}
func (list *ArrayList)Get(index int) (interface{}, error) {
if index < 0 || index > list.TheSize {
return nil,errors.New("索引越界")
}
return list.dataStore[index],nil
}
func (list *ArrayList) Append(newVal interface{}) {
list.dataStore = append(list.dataStore, newVal)
list.TheSize++
}
func (list *ArrayList) String()string {
return fmt.Sprint(list.dataStore)
}
func (list *ArrayList) Set(index int, newVal interface{})error {
if index < 0 || index > list.TheSize {
return errors.New("索引越界")
}
list.dataStore[index] = newVal
return nil
}
func (list *ArrayList) Insert(index int, newVal interface{})error {
if index < 0 || index > list.TheSize {
return errors.New("索引越界")
}
list.checkIsFull()
list.dataStore = list.dataStore[:list.TheSize+1]
for i := list.TheSize; i > index; i-- {
list.dataStore[i] = list.dataStore[i - 1]
}
list.dataStore[index] = newVal
list.TheSize++
return nil
}
func (list *ArrayList) Clear() {
list.dataStore = make([]interface{}, 0, 10)
list.TheSize = 0
}
func (list *ArrayList) Delete(index int)error {
if index < 0 || index > list.TheSize {
return errors.New("索引越界")
}
list.dataStore = append(list.dataStore[:index], list.dataStore[index+1:]...)
list.TheSize--
return nil
}
package main
import (
"godemo01/ArrayList"
"fmt"
)
func main() {
list := ArrayList.NewArrayList()
list.Append(1)
list.Append(2)
list.Append(3)
for i := 0; i < 10; i++ {
list.Insert(1, i)
}
list.Delete(2)
fmt.Println(list)
fmt.Println(list.Size())
list.Clear();
fmt.Println(list.TheSize)
list.String()
}
小技巧 数组迭代器
- 解决数组访问方便得问题
迭代器模式-常见操作可能有多个类的嵌套
- 增加Iterator
package ArrayList
import (
"errors"
"fmt"
)
type List interface {
Size() int
Get(index int)(interface{}, error)
Set(index int, newVal interface{})error
Insert(index int, newVal interface{})error
Append(newVal interface{})
Clear()
Delete(index int)error
String()string
Iterator() Iterator
}
type ArrayList struct {
dataStore [] interface{}
TheSize int
}
func NewArrayList()* ArrayList{
list := new(ArrayList)
list.dataStore = make([]interface{}, 0, 10)
list.TheSize = 0
return list
}
func (list *ArrayList)checkIsFull() {
if list.TheSize == cap(list.dataStore) {
newDateStore := make([]interface{}, list.TheSize,2*list.TheSize)
copy(newDateStore, list.dataStore)
list.dataStore = newDateStore
}
}
func (list *ArrayList)Size() int {
return list.TheSize
}
func (list *ArrayList)Get(index int) (interface{}, error) {
if index < 0 || index > list.TheSize {
return nil,errors.New("索引越界")
}
return list.dataStore[index],nil
}
func (list *ArrayList) Append(newVal interface{}) {
list.dataStore = append(list.dataStore, newVal)
list.TheSize++
}
func (list *ArrayList) String()string {
return fmt.Sprint(list.dataStore)
}
func (list *ArrayList) Set(index int, newVal interface{})error {
if index < 0 || index > list.TheSize {
return errors.New("索引越界")
}
list.dataStore[index] = newVal
return nil
}
func (list *ArrayList) Insert(index int, newVal interface{})error {
if index < 0 || index > list.TheSize {
return errors.New("索引越界")
}
list.checkIsFull()
list.dataStore = list.dataStore[:list.TheSize+1]
for i := list.TheSize; i > index; i-- {
list.dataStore[i] = list.dataStore[i - 1]
}
list.dataStore[index] = newVal
list.TheSize++
return nil
}
func (list *ArrayList) Clear() {
list.dataStore = make([]interface{}, 0, 10)
list.TheSize = 0
}
func (list *ArrayList) Delete(index int)error {
if index < 0 || index > list.TheSize {
return errors.New("索引越界")
}
list.dataStore = append(list.dataStore[:index], list.dataStore[index+1:]...)
list.TheSize--
return nil
}
package ArrayList
import (
"errors"
)
type Iterator interface {
HasNext () bool
Next()(interface{}, error)
Remove()
GetIndex() int
}
type Iterable interface {
Iterator() Iterator
}
type ArrayListIterator struct {
list *ArrayList
currentIndex int
}
func (list *ArrayList) Iterator() Iterator {
it := new(ArrayListIterator)
it.currentIndex = 0
it.list = list
return it
}
func (it *ArrayListIterator)HasNext () bool{
return it.currentIndex < it.list.TheSize
}
func (it *ArrayListIterator)Next()(interface{}, error){
if !it.HasNext() {
return nil, errors.New("没有下一项了")
}
value, err := it.list.Get(it.currentIndex)
it.currentIndex++
return value, err
}
func (it *ArrayListIterator)Remove(){
it.currentIndex--
it.list.Delete(it.currentIndex)
}
func (it *ArrayListIterator)GetIndex() int{
return it.currentIndex
}
package main
import (
"godemo01/ArrayList"
"fmt"
)
func main() {
var list ArrayList.List = ArrayList.NewArrayList()
list.Append(1)
list.Append(2)
list.Append(3)
list.Append(4)
list.Append(5)
for it := list.Iterator(); it.HasNext(); {
item,_ := it.Next()
if item == 3 {
it.Remove()
}
}
fmt.Println(list)
}