Documentation
¶
Overview ¶
Package arraylist provides a resizable array implementation.
Index ¶
- type ArrayList
- func (l ArrayList[T]) At(index int) (element T, err error)
- func (l *ArrayList[T]) Clear() *ArrayList[T]
- func (l ArrayList[T]) Concat(another ArrayList[T]) ArrayList[T]
- func (l ArrayList[T]) Copy() ArrayList[T]
- func (l ArrayList[T]) Count(element T) (count int)
- func (l ArrayList[T]) Empty() bool
- func (l ArrayList[T]) Equal(another ArrayList[T]) bool
- func (l ArrayList[T]) Every(condition func(T) bool) bool
- func (l *ArrayList[T]) Fill(element T, area ...int) *ArrayList[T]
- func (l ArrayList[T]) Filter(condition func(T) bool) ArrayList[T]
- func (l ArrayList[T]) Find(by func(T) bool) (element T, found bool)
- func (l ArrayList[T]) FindIndex(by func(T) bool) (index int)
- func (l ArrayList[T]) FindIndexes(by func(T) bool, counts ...int) (indexes []int)
- func (l ArrayList[T]) FindLast(by func(T) bool) (element T, found bool)
- func (l ArrayList[T]) FindLastIndex(by func(T) bool) (index int)
- func (l ArrayList[T]) FindLastIndexes(by func(T) bool, counts ...int) (indexes []int)
- func (l ArrayList[T]) FindLasts(by func(T) bool, counts ...int) (elements []T)
- func (l ArrayList[T]) Finds(by func(T) bool, counts ...int) (elements []T)
- func (l *ArrayList[T]) ForEach(action func(T) T) *ArrayList[T]
- func (l ArrayList[T]) Head() (element T, err error)
- func (l ArrayList[T]) Includes(element T) bool
- func (l ArrayList[T]) IndexOf(element T) (index int)
- func (l *ArrayList[T]) Insert(index int, element T) *ArrayList[T]
- func (l ArrayList[T]) LastIndexOf(element T) (index int)
- func (l ArrayList[T]) Len() int
- func (l ArrayList[T]) Map(handler func(T) T) ArrayList[T]
- func (l *ArrayList[T]) Pop(indexes ...int) (element T, err error)
- func (l *ArrayList[T]) Push(elements ...T) (length int)
- func (l ArrayList[T]) Reduce(handler func(T, T) T, initial ...T) (result T, err error)
- func (l ArrayList[T]) ReduceRight(handler func(T, T) T, initial ...T) (result T, err error)
- func (l *ArrayList[T]) Remove(element T, counts ...int) *ArrayList[T]
- func (l *ArrayList[T]) RemoveIf(condition func(T) bool, counts ...int) ArrayList[T]
- func (l *ArrayList[T]) RemoveRight(element T, counts ...int) *ArrayList[T]
- func (l *ArrayList[T]) RemoveRightIf(condition func(T) bool, counts ...int) ArrayList[T]
- func (l *ArrayList[T]) Replace(oldElement, newElement T, counts ...int) *ArrayList[T]
- func (l *ArrayList[T]) ReplaceIf(condition func(T) bool, newElement T, counts ...int) ArrayList[T]
- func (l *ArrayList[T]) ReplaceRight(oldElement, newElement T, counts ...int) *ArrayList[T]
- func (l *ArrayList[T]) ReplaceRightIf(condition func(T) bool, newElement T, counts ...int) ArrayList[T]
- func (l *ArrayList[T]) Reverse() *ArrayList[T]
- func (l *ArrayList[T]) Set(index int, element T) (err error)
- func (l *ArrayList[T]) Shift() (element T, err error)
- func (l ArrayList[T]) Slice(args ...int) ArrayList[T]
- func (l ArrayList[T]) Some(condition func(T) bool) bool
- func (l *ArrayList[T]) Splice(start, deleteCount int, elements ...T) ArrayList[T]
- func (l ArrayList[T]) Tail() (element T, err error)
- func (l ArrayList[T]) ToReversed() ArrayList[T]
- func (l ArrayList[T]) ToSpliced(start, deleteCount int, items ...T) ArrayList[T]
- func (l *ArrayList[T]) Unshift(elements ...T) (length int)
- func (l ArrayList[T]) With(index int, element T) ArrayList[T]
Examples ¶
- ArrayList
- ArrayList.At
- ArrayList.Clear
- ArrayList.Concat
- ArrayList.Copy
- ArrayList.Count
- ArrayList.Empty
- ArrayList.Equal
- ArrayList.Every
- ArrayList.Fill
- ArrayList.Filter
- ArrayList.Find
- ArrayList.FindIndex
- ArrayList.FindIndexes
- ArrayList.FindLast
- ArrayList.FindLastIndex
- ArrayList.FindLastIndexes
- ArrayList.FindLasts
- ArrayList.Finds
- ArrayList.ForEach
- ArrayList.Head
- ArrayList.Includes
- ArrayList.IndexOf
- ArrayList.Insert
- ArrayList.LastIndexOf
- ArrayList.Len
- ArrayList.Map
- ArrayList.Pop
- ArrayList.Push
- ArrayList.Reduce
- ArrayList.ReduceRight
- ArrayList.Remove
- ArrayList.RemoveIf
- ArrayList.RemoveRight
- ArrayList.RemoveRightIf
- ArrayList.Replace
- ArrayList.ReplaceIf
- ArrayList.ReplaceRight
- ArrayList.ReplaceRightIf
- ArrayList.Reverse
- ArrayList.Set
- ArrayList.Shift
- ArrayList.Slice
- ArrayList.Some
- ArrayList.Splice
- ArrayList.Tail
- ArrayList.ToReversed
- ArrayList.ToSpliced
- ArrayList.Unshift
- ArrayList.With
- Of
- Repeat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArrayList ¶
type ArrayList[T any] []T
ArrayList is a generic type representing a list of elements
Example ¶
al := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(al)
Output: [1 2 3 4 5]
func Of ¶
Of creates a new ArrayList with the provided elements.
Example ¶
list := Of(1, 2, 3, 4, 5) fmt.Println(list)
Output: [1 2 3 4 5]
func Repeat ¶
Repeat creates a new ArrayList by repeating the specified element for the given count.
Example ¶
repeated := Repeat("hello", 3) fmt.Println(repeated)
Output: [hello hello hello]
func (ArrayList[T]) At ¶
At returns the element at the specified index in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} element, _ := list.At(2) fmt.Println(element) element, _ = list.At(-1) fmt.Println(element) _, err := list.At(10) fmt.Println(err)
Output: 3 5 the index is out of range
func (*ArrayList[T]) Clear ¶
Clear removes all elements from the list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list) list.Clear() fmt.Println(list)
Output: [1 2 3 4 5] []
func (ArrayList[T]) Concat ¶
Concat concatenates the current ArrayList with another ArrayList.
Example ¶
list1 := ArrayList[int]{1, 2, 3} list2 := ArrayList[int]{4, 5, 6} concatenated := list1.Concat(list2) fmt.Println(concatenated)
Output: [1 2 3 4 5 6]
func (ArrayList[T]) Copy ¶
Copy creates a shallow copy of the ArrayList.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5} backup := l.Copy() fmt.Println(backup) fmt.Println(l.Equal(backup))
Output: [1 2 3 4 5] true
func (ArrayList[T]) Count ¶
Count returns the number of occurrences of the specified element in the list
Example ¶
al := ArrayList[int]{1, 2, 3, 4, 5, 5, 5} fmt.Println(al.Count(5)) fmt.Println(al.Count(6))
Output: 3 0
func (ArrayList[T]) Empty ¶
Empty checks if the list is empty
Example ¶
al := ArrayList[int]{} fmt.Println(al.Empty()) al2 := ArrayList[int]{1, 2, 3} fmt.Println(al2.Empty())
Output: true false
func (ArrayList[T]) Equal ¶
Equal checks if two lists are equal by comparing their elements
Example ¶
al1 := ArrayList[int]{1, 2, 3} al2 := ArrayList[int]{1, 2, 3} al3 := ArrayList[int]{1, 2, 3, 4} al4 := ArrayList[int]{1, 2, 4} fmt.Println(al1.Equal(al2)) fmt.Println(al1.Equal(al3)) fmt.Println(al1.Equal(al4))
Output: true false false
func (ArrayList[T]) Every ¶
Every checks if every item in the ArrayList satisfies the condition function.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} allEven := list.Every(func(item int) bool { return item%2 == 0 }) fmt.Println(allEven) allPositive := list.Every(func(item int) bool { return item > 0 }) fmt.Println(allPositive)
Output: false true
func (*ArrayList[T]) Fill ¶
Fill fills the specified area with the specified element. If the area argument is not provided, the entire list will be filled with the specified element.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list) list.Fill(0, 1, 3) fmt.Println(list)
Output: [1 2 3 4 5] [1 0 0 4 5]
func (ArrayList[T]) Filter ¶
Filter creates a new ArrayList with all items that pass the condition function.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} condition := func(item int) bool { return item%2 == 0 } filteredList := list.Filter(condition) fmt.Println(filteredList)
Output: [2 4]
func (ArrayList[T]) Find ¶
Find returns the first element that satisfies the given condition in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} even := func(num int) bool { return num%2 == 0 } fmt.Println(list.Find(even)) negative := func(num int) bool { return num < 0 } fmt.Println(list.Find(negative))
Output: 2 true 0 false
func (ArrayList[T]) FindIndex ¶
FindIndex returns the index of the first element that satisfies the given condition in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} even := func(num int) bool { return num%2 == 0 } fmt.Println(list.FindIndex(even)) negative := func(num int) bool { return num < 0 } fmt.Println(list.FindIndex(negative))
Output: 1 -1
func (ArrayList[T]) FindIndexes ¶
FindIndexes returns the indexes of elements that satisfy the given condition in the array list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5, 2} condition := func(val int) bool { return val > 2 } fmt.Println(l.FindIndexes(condition)) fmt.Println(l.FindIndexes(condition, 2))
Output: [2 3 4] [2 3]
func (ArrayList[T]) FindLast ¶
FindLast returns the last element that satisfies the given condition in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} even := func(num int) bool { return num%2 == 0 } fmt.Println(list.Find(even)) fmt.Println(list.FindLast(even))
Output: 2 true 4 true
func (ArrayList[T]) FindLastIndex ¶
FindLastIndex returns the index of the last element that satisfies the given condition in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} even := func(num int) bool { return num%2 == 0 } fmt.Println(list.FindIndex(even)) fmt.Println(list.FindLastIndex(even))
Output: 1 3
func (ArrayList[T]) FindLastIndexes ¶
FindLastIndexes returns the indexes of the last elements that satisfy the given condition in the array list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5, 2} condition := func(val int) bool { return val > 2 } fmt.Println(l.FindIndexes(condition)) fmt.Println(l.FindLastIndexes(condition))
Output: [2 3 4] [4 3 2]
func (ArrayList[T]) FindLasts ¶
FindLasts returns the last elements that satisfy the given condition in the array list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5, 2} condition := func(val int) bool { return val > 2 } fmt.Println(l.Finds(condition)) fmt.Println(l.FindLasts(condition))
Output: [3 4 5] [5 4 3]
func (ArrayList[T]) Finds ¶
Finds returns the elements that satisfy the given condition in the array list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5, 2} condition := func(val int) bool { return val > 2 } fmt.Println(l.Finds(condition)) fmt.Println(l.Finds(condition, 2))
Output: [3 4 5] [3 4]
func (*ArrayList[T]) ForEach ¶
ForEach applies the specified action to each element of the list. Returns the modified list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list) list.ForEach(func(item int) int { return item * 2 }) fmt.Println(list)
Output: [1 2 3 4 5] [2 4 6 8 10]
func (ArrayList[T]) Head ¶
Head returns the first element of the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list.Head()) fmt.Println(ArrayList[int]{}.Head())
Output: 1 <nil> 0 the input list is empty
func (ArrayList[T]) Includes ¶
Includes checks if the specified element is present in the list
Example ¶
al := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(al.Includes(3)) fmt.Println(al.Includes(6))
Output: true false
func (ArrayList[T]) IndexOf ¶
IndexOf returns the index of the first occurrence of the given element in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list.IndexOf(3)) fmt.Println(list.IndexOf(6))
Output: 2 -1
func (*ArrayList[T]) Insert ¶
Insert inserts the specified element at the specified index. If the index is negative, the index will be calculated from the end of the list. Returns the modified list.
Example ¶
list := ArrayList[int]{1, 2, 3} fmt.Println(list) list.Insert(1, 5) fmt.Println(list)
Output: [1 2 3] [1 5 2 3]
func (ArrayList[T]) LastIndexOf ¶
LastIndexOf returns the index of the last occurrence of the given element in the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 3} fmt.Println(list.IndexOf(3)) fmt.Println(list.LastIndexOf(3))
Output: 2 4
func (ArrayList[T]) Len ¶
Len returns the length of the list
Example ¶
al := ArrayList[int]{1, 2, 3, 4, 5} length := al.Len() fmt.Println(length)
Output: 5
func (ArrayList[T]) Map ¶
Map applies the given handler function to each item in the ArrayList and returns a new ArrayList of the results.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} handler := func(val int) int { return val * 2 } newList := list.Map(handler) fmt.Println(newList) fmt.Println(list)
Output: [2 4 6 8 10] [1 2 3 4 5]
func (*ArrayList[T]) Pop ¶
Pop removes and returns the last element of the list. If the indexes argument is provided, the element at the specified index will be removed. Returns the removed element and an error if the index is out of range.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(l) // Remove the last element element, _ := l.Pop() fmt.Println(element) fmt.Println(l) // Remove the element at a specific index element, _ = l.Pop(2) fmt.Println(element) fmt.Println(l)
Output: [1 2 3 4 5] 5 [1 2 3 4] 3 [1 2 4]
func (*ArrayList[T]) Push ¶
Push adds one or more elements to the end of the list.
Example ¶
list := ArrayList[int]{1, 2, 3} fmt.Println(list) list.Push(4, 5, 6) fmt.Println(list)
Output: [1 2 3] [1 2 3 4 5 6]
func (ArrayList[T]) Reduce ¶
Reduce reduces the ArrayList to a single value by applying the handler function cumulatively to each item, starting with the initial value if provided.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} // Example 1: Summing up the elements of the list sum, _ := list.Reduce(func(a, b int) int { return a + b }) fmt.Println(sum) // Example 2: Finding the maximum element in the list max, _ := list.Reduce(func(a, b int) int { if a > b { return a } return b }, 0) fmt.Println(max) // Example 3: Summing up the elements of the list with initial value sum, _ = list.Reduce(func(a, b int) int { return a + b }, 10) fmt.Println(sum)
Output: 15 5 25
func (ArrayList[T]) ReduceRight ¶
ReduceRight reduces the ArrayList to a single value by applying the handler function cumulatively from right to left, starting with the initial value if provided.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} f := func(a, b int) int { return a - b } result1, _ := list.Reduce(f) fmt.Println(result1) result2, _ := list.ReduceRight(f) fmt.Println(result2)
Output: -13 -5
func (*ArrayList[T]) Remove ¶
Remove removes the occurrences of the specified element from the list. If the count argument is provided, only the first count occurrences will be removed. Returns the modified list.
Example ¶
l := ArrayList[int]{1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5} fmt.Println(l) l.Remove(2) fmt.Println(l) l.Remove(4, -1) fmt.Println(l) l.Remove(5, 2) fmt.Println(l)
Output: [1 2 2 3 4 4 4 5 5 5 5 5] [1 2 3 4 4 4 5 5 5 5 5] [1 2 3 5 5 5 5 5] [1 2 3 5 5 5]
func (*ArrayList[T]) RemoveIf ¶
RemoveIf removes the elements that satisfy the specified condition. If the count argument is not provided, only the first count occurrences will be removed. Returns the removed elements.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} evenCondition := func(val int) bool { return val%2 == 0 } fmt.Println(list) removed := list.RemoveIf(evenCondition, -1) fmt.Println(list) fmt.Println(removed)
Output: [1 2 3 4 5 6 7 8 9 10] [1 3 5 7 9] [2 4 6 8 10]
func (*ArrayList[T]) RemoveRight ¶
RemoveRight removes the occurrences of the specified element from the right end of the list. If the count argument is provided, only the first count occurrences will be removed. Returns the modified list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5, 4, 3, 2, 1} l2 := l.Copy() fmt.Println(l) l.Remove(4) l2.RemoveRight(4) fmt.Println(l) fmt.Println(l2)
Output: [1 2 3 4 5 4 3 2 1] [1 2 3 5 4 3 2 1] [1 2 3 4 5 3 2 1]
func (*ArrayList[T]) RemoveRightIf ¶
RemoveRightIf removes the elements that satisfy the specified condition from the right end of the list. If the count argument is not provided, only the first count occurrences will be removed. Returns the removed elements.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} list2 := list.Copy() condition := func(val int) bool { return val%2 == 0 } fmt.Println(list) removed1 := list.RemoveRightIf(condition, 3) removed2 := list2.RemoveIf(condition, 3) fmt.Println(list) fmt.Println(removed1) fmt.Println(list2) fmt.Println(removed2)
Output: [1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 7 9] [10 8 6] [1 3 5 7 8 9 10] [2 4 6]
func (*ArrayList[T]) Replace ¶
Replace replaces occurrences of the specified old element with the specified new element. If the count argument is not provided, only the first count occurrences will be replaced. Returns the modified list.
Example ¶
l := ArrayList[int]{1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5} fmt.Println(l) l.Replace(2, -2) fmt.Println(l) l.Replace(4, -4, -1) fmt.Println(l) l.Replace(5, -5, 2) fmt.Println(l)
Output: [1 2 2 3 4 4 4 5 5 5 5 5] [1 -2 2 3 4 4 4 5 5 5 5 5] [1 -2 2 3 -4 -4 -4 5 5 5 5 5] [1 -2 2 3 -4 -4 -4 -5 -5 5 5 5]
func (*ArrayList[T]) ReplaceIf ¶
ReplaceIf replaces the elements that satisfy the specified condition with the specified new element. If the count argument is not provided, only the first count occurrences will be replaced. Returns the replaced elements.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} evenCondition := func(val int) bool { return val%2 == 0 } fmt.Println(list) replaced := list.ReplaceIf(evenCondition, -1, -1) fmt.Println(list) fmt.Println(replaced)
Output: [1 2 3 4 5 6 7 8 9 10] [1 -1 3 -1 5 -1 7 -1 9 -1] [2 4 6 8 10]
func (*ArrayList[T]) ReplaceRight ¶
ReplaceRight replaces occurrences of the specified old element with the specified new element from the right end of the list. If the count argument is not provided, only the first count occurrences will be replaced. Returns the modified list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5, 4, 3, 2, 1} l2 := l.Copy() fmt.Println(l) l.Replace(4, 0) l2.ReplaceRight(4, 0) fmt.Println(l) fmt.Println(l2)
Output: [1 2 3 4 5 4 3 2 1] [1 2 3 0 5 4 3 2 1] [1 2 3 4 5 0 3 2 1]
func (*ArrayList[T]) ReplaceRightIf ¶
func (l *ArrayList[T]) ReplaceRightIf(condition func(T) bool, newElement T, counts ...int) ArrayList[T]
ReplaceRightIf replaces the elements that satisfy the specified condition with the specified new element from the right end of the list. If the count argument is not provided, only the first count occurrences will be replaced. Returns the replaced elements.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} list2 := list.Copy() condition := func(val int) bool { return val%2 == 0 } fmt.Println(list) replaced1 := list.ReplaceRightIf(condition, 0, 3) replaced2 := list2.ReplaceIf(condition, 0, 3) fmt.Println(list) fmt.Println(replaced1) fmt.Println(list2) fmt.Println(replaced2)
Output: [1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 0 7 0 9 0] [10 8 6] [1 0 3 0 5 0 7 8 9 10] [2 4 6]
func (*ArrayList[T]) Reverse ¶
Reverse reverses the order of the elements in the list.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(l) l.Reverse() fmt.Println(l)
Output: [1 2 3 4 5] [5 4 3 2 1]
func (*ArrayList[T]) Set ¶
Set sets the element at the specified index to the specified element. If the index is negative, the index will be calculated from the end of the list. Returns an error if the index is out of range.
Example ¶
list := ArrayList[int]{1, 2, 3} fmt.Println(list) _ = list.Set(2, 6) fmt.Println(list) _ = list.Set(-2, 5) fmt.Println(list) err := list.Set(5, 6) fmt.Println(err) fmt.Println(list)
Output: [1 2 3] [1 2 6] [1 5 6] the index is out of range [1 5 6]
func (*ArrayList[T]) Shift ¶
Shift removes and returns the first element of the list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list) element, _ := list.Shift() fmt.Println(element) fmt.Println(list)
Output: [1 2 3 4 5] 1 [2 3 4 5]
func (ArrayList[T]) Slice ¶
Slice returns a new ArrayList by slicing the current ArrayList based on the provided arguments.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} slice1 := list.Slice(1, 4) fmt.Println(slice1) slice2 := list.Slice(0, 3, 2) fmt.Println(slice2) slice3 := list.Slice(3, 0, -1) fmt.Println(slice3)
Output: [2 3 4] [1 3] [4 3 2]
func (ArrayList[T]) Some ¶
Some checks if at least one item in the ArrayList satisfies the condition function.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} result := list.Some(func(i int) bool { return i > 3 }) fmt.Println(result) result = list.Some(func(i int) bool { return i > 5 }) fmt.Println(result)
Output: true false
func (*ArrayList[T]) Splice ¶
Splice replaces the specified elements with the specified new elements.
Example ¶
arr := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(arr) arr.Splice(2, 2, 6, 7, 8) fmt.Println(arr)
Output: [1 2 3 4 5] [1 2 6 7 8 5]
func (ArrayList[T]) Tail ¶
Tail returns the last element of the array list.
Example ¶
list := ArrayList[int]{1, 2, 3, 4, 5} fmt.Println(list.Tail()) fmt.Println(ArrayList[int]{}.Tail())
Output: 5 <nil> 0 the input list is empty
func (ArrayList[T]) ToReversed ¶
ToReversed returns a new ArrayList with its elements reversed compared to the current ArrayList.
Example ¶
l := ArrayList[int]{1, 2, 3, 4, 5} reversed := l.ToReversed() fmt.Println(reversed) fmt.Println(l)
Output: [5 4 3 2 1] [1 2 3 4 5]
func (ArrayList[T]) ToSpliced ¶
ToSpliced returns a new ArrayList after applying the splice operation to the current ArrayList.
Example ¶
arr := ArrayList[int]{1, 2, 3, 4, 5} newArr := arr.ToSpliced(2, 2, 6, 7, 8) fmt.Println(newArr) fmt.Println(arr)
Output: [1 2 6 7 8 5] [1 2 3 4 5]
func (*ArrayList[T]) Unshift ¶
Unshift adds one or more elements to the beginning of the list.
Example ¶
l := ArrayList[int]{1, 2, 3} fmt.Println(l) l.Unshift(4, 5) fmt.Println(l)
Output: [1 2 3] [4 5 1 2 3]