项目作者: CorentinLeGuen

项目描述 :
A simple golang arraylist with string, int and interface{} arraylist tools
高级语言: Go
项目地址: git://github.com/CorentinLeGuen/arraylist.git
创建时间: 2019-11-12T20:10:01Z
项目社区:https://github.com/CorentinLeGuen/arraylist

开源协议:

下载


ArrayList

This library is using Go slices and arrays to provide a Java arraylist implementation like.

Don’t hesitate to email me (leguen.corentin@protonmail.com) or contribute to the project !

Go Get

:construction: Presently under construction

How to use

Constructor

To create a new arraylist : New() *List

Methods

Signature Description
Len() int Return the length of the arraylist
Clear() Empty the arraylist
Clone() *List Return a copy of the arraylist
Sublist(fromRange int, toRange int) *List Return the inclusive sublist [start, end] of an arraylist
IndexOf(elem interface{}) int Return the position of elem in the arraylist
GetValue(position int) interface{} Return the value at the position position
Contains(elem interface{}) bool Show if the value elem is in the arraylist
Add(elem interface{}) Add elem to the tail of the arraylist
AddAll(elems []interface{}) Add all elems to the tail of the arraylist
ReplaceAll(fromElem interface{}, toElem interface{}) Replace all occurrences of an element by another one
RemoveFirst(elem interface{}) Remove the first occurrence of elem from the arraylist
RemoveAtIndex(index int) Remove the item at the position index
RemoveAll(elem interface{}) Remove all occurrences of elem from the arraylist
ToArray() []interface{} Return the arraylist as an array
Equals(o *List) bool Compare two arraylist

Example

  1. package main
  2. import (
  3. "fmt"
  4. arrayList "github.com/CorentinLeGuen/arraylist"
  5. )
  6. func main() {
  7. s := arrayList.New()
  8. s.Add("toto")
  9. s.Add("titi")
  10. s.Add("toto")
  11. s.Add("tutu")
  12. s.Add("toto") // add five strings to the arraylist
  13. fmt.Printf("%t\n", s.Contains("tata")) // false
  14. fmt.Printf("%t\n", s.Contains("toto")) // true
  15. s.RemoveFirst("toto") // we are removing the first "toto"
  16. fmt.Printf("%t\n", s.Contains("toto")) // true
  17. s.RemoveAll("toto") // we are removing every "toto"
  18. fmt.Printf("%t\n", s.Contains("toto")) // false
  19. fmt.Printf("%d\n", s.Len()) // 2 there is only ["titi", "tutu"] in the arraylist
  20. s.AddAll([]interface{}{"toto", "tata"}) // adding at the end of the arraylist
  21. fmt.Printf("%t\n", s.Contains("tata")) // true
  22. fmt.Printf("%d\n", s.GetPosition("tutu")) // 1
  23. fmt.Printf("%s\n", s.GetValue(2)) // "toto"
  24. }

You can take a look at tests files for more examples.

Todo

  • Update the README with go get and example import