ArrayList in Go

本文详细介绍了如何在Go语言中实现ArrayList,包括接口定义、数据结构设计、ArrayList的创建以及各种方法的实现。通过阅读,读者将了解到一个完整的ArrayList在Go中的具体实现过程。
摘要由CSDN通过智能技术生成

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("索引越界")
	
帮我写出以下java代码:The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1. WARNING: when you use the remove method of Java’s ArrayList class to remove an element of an arraylist at index i, the arraylist immediately shifts down by one position all the elements with higher indexes to make the arraylist one element shorter. So, for example, when removing the element at index i, the element at index i+1 immediately moves to the position at index i, the element at index i+2 immediately moves to the position at index i+1, etc. This means that on the next iteration of the loop, when i has become i+1, the element that you will be testing at index i+1 is in fact the element that used to be at index i+2. Which means that the element that used to be at index i+1 (and which is now at index i) will never be tested! Therefore, when removing elements from an arraylist, if your loop starts at index 0 and goes up the indexes in the arraylist, then your loop will fail to test some elements! CONCLUSION: when removing elements from an arraylist, your loop must start from the END of the arraylist and go DOWN to index 0. The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bubbles. Make sure you test as many methods of the Model class as poss
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值