ArrayList in Go

ArrayList 的 Go 語言實現

接口

type List interface {
	Size() int
	Get(index int) (interface{}, error)
	Set(index int, newValue interface{}) error
	Insert(index int, val interface{}) error
	Append(val ...interface{})
	Delete(index int) error
	String() string
	Clear()
}

數據結構

type ArrayList struct {
	DataStore []interface{}
	TheSize int
}

新建一個ArrayList

// 新建
func NewArrayList() *ArrayList {
	// func new(Type) *Type
	list := new(ArrayList)
	list.DataStore = make([]interface{}, 0, 10)
	list.TheSize = 0
	return list
}

方法實現


// 返回長度
func (list *ArrayList) Size() int {
	return list.TheSize
}
// 獲取數據
func (list *ArrayList) Get(index int) (interface{}, error) {
	if index < 0 || index >= len(list.DataStore) {
		return nil, 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
	return  nil
}
// 插入元素
// 是否空間不足
func checkIsFull(list *ArrayList) {
	if len(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++
	return nil
}
// 追加數據
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--
	return nil
}
// 字符打印
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"
)

func main() {
	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() int
	Get(index int) (interface{}, error)
	Set(index int, newValue interface{}) error
	Insert(index int, val interface{}) error
	Append(val ...interface{})
	Delete(index int) error
	String() string
	Clear()
}

// 數據結構
type ArrayList struct {
	DataStore []interface{}
	TheSize int
}
// 新建
func NewArrayList() *ArrayList {
	// func new(Type) *Type
	list := new(ArrayList)
	list.DataStore = make([]interface{}, 0, 10)
	list.TheSize = 0
	return list
}
// 返回長度
func (list *ArrayList) Size() int {
	return list.TheSize
}
// 獲取數據
func (list *ArrayList) Get(index int) (interface{}, error) {
	if index < 0 || index >= len(list.DataStore) {
		return nil, 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
	return  nil
}
// 插入元素
// 是否空間不足
func checkIsFull(list *ArrayList) {
	if len(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++
	return nil
}
// 追加數據
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--
	return nil
}
// 字符打印
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
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章