-
Notifications
You must be signed in to change notification settings - Fork 2
kiwi.util.ArrayList
ArrayList is a resizable-array implementation of the List interface. Implements all optional list operations, permits all element types and maintains insertion order.
The ArrayList class exists in the kiwi/util/ArrayList.bi file.
ArrayList can hold elements of FreeBasic standard types (Byte, UByte, Short, UShort, Long, Ulong, Integer, UInteger, LongInt, ULongInt, Single, Double, Boolean, String) and KObject.
To use User Defined Types (UDT), you must call the MACRO_DefineArrayList macro before you initialize an array of that type. Read the second example on this page to learn more.
Method | Description |
---|---|
ArrayList.add(byref e as list_type) | Appends the specified element to the end of this list. |
ArrayList.add(index as UInteger, byref e as list_type) | Inserts the specified element at the specified position in this list.Shifts the element currently at that position (if any) and anysubsequent elements to the right (adds one to their indices). |
ArrayList.addAll(byref c as Collection) | Adds all of the elements of the give collection to this collection. |
ArrayList.remove(byval index as Integer) | Removes the element at the specified position in this list and returns it |
ArrayList.indexOf(byref e as list_type) | Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
ArrayList.contains(byref e as list_type) | Returns true if this list contains the specified element. |
ArrayList.set(byval index as integer, byref element as list_type) | Replaces the element at the specified position in this list with the specified element and returns the element previously at the specified position. |
ArrayList.size() | Returns the number of elements in this ArrayList. |
ArrayList.isEmpty() | Returns true if this ArrayList contains no elements. |
ArrayList.sort(byref c as Comparator) | Sorts this ArrayList according to the order induced by the specified Comparator. |
ArrayList.clean() | Removes all of the elements from this ArrayList. The ArrayList will be empty after this call returns. |
#include once "kiwi\kiwi.bi"
' Initialize a new ArrayList for String elements
Dim myStringArrayList As ArrayList(String)
' Add Data To myStringArrayList
myStringArrayList.add("FreeBasic")
myStringArrayList.add("Array")
myStringArrayList.add("List")
print "ArrayList contains " & myStringArrayList.size() & " elements"
print ""
print "Array List Data:"
for i as Integer = 0 to myStringArrayList.size()-1
print "Element " & i &" = " & myStringArrayList.get(i)
next
#include once "kiwi\kiwi.bi"
' In this example we will create an ArrayList that holds Students
Type Student extends KObject ' Always inherit from KObject for all UDTs
firstName as String
lastName as String
End Type
' Tells FreeBasic, that you want to use an ArrayList with "Student" variables
MACRO_DefineArrayList(Student)
' Initialize a new ArrayList to hold students
Dim students as ArrayList(Student)
Dim student1 as student
student1.firstName = "Nikos"
student1.lastName = "Siatras"
students.Add(student1) ' Add student1 to students ArrayList
Dim student2 As student
student2.firstName = "Elon"
student2.lastName = "Musk"
students.Add(student2) ' Add student2 to students ArrayList
print "Students ArrayList contains " & students.size() & " elements"
print ""
print "Students: "
for i as Integer = 0 to students.size() - 1
print "Student " & i & " = " & students.get(i).firstName & " " & students.get(i).lastName
next i
The following example will sort an ArrayList holding 10 random double values.
#include once "kiwi\kiwi.bi"
' Initialize a new ArrayList for Double elements
Dim myArrayList as ArrayList(Double)
' Add 10 Random double values to myArrayList
for i as Integer = 0 to 9
myArrayList.add(Math.random())
next i
print "ArrayList Elements Before Sort:"
for i as Integer = 0 to myArrayList.size()-1
print "Element " & i &" = " & myArrayList.get(i)
next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Create a Comparator(Double) in order to sort the array
Type myComparator extends Comparator(Double)
declare function compare(a as Double, b as Double) as Integer
End Type
function myComparator.compare(a as Double, b as Double) as Integer
if a = b then
return 0
else
return iif(a>b, 1, -1) ' Ascending
'return iif(a<b , 1, -1) 'Descending
end if
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Sort the array using the comparator
myArrayList.sort(myComparator)
' Print the Sorted Data of the ArrayList
print ""
print "ArrayList Elements After Sort:"
for i as Integer = 0 to myArrayList.size()-1
print "Element " & i &" = " & myArrayList.get(i)
next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Sort the array using the comparator
myArrayList.sort(myComparator)
' Print the Sorted Data of the ArrayList
print ""
print "ArrayList Elements After Sort:"
for i as Integer = 0 to myArrayList.size()-1
print "Element " & i &" = " & myArrayList.get(i)
next
The following example will add 3 "Student" type objects to an ArrayList and then will sort it by Student.grade.
#include once "kiwi\kiwi.bi"
' In this example we will create an ArrayList that holds Students
Type Student extends KObject ' Always inherit from KObject for all UDTs
firstName as String
lastName As String
grade as Double
End Type
' Define a new ArrayList type that hold's student using the
' macro command "MACRO_DefineArrayList"
MACRO_DefineArrayList(Student)
' Initialize a new ArrayList to hold students
Dim studentsList As ArrayList(Student)
Dim student1 As student
student1.firstName = "Nikos"
student1.lastName = "Siatras"
student1.grade = 9.5
studentsList.Add(student1) ' Add student1 to students ArrayList
Dim student2 As student
student2.firstName = "Elon"
student2.lastName = "Musk"
student2.grade = 8.9
studentsList.Add(student2) ' Add student2 to students ArrayList
Dim student3 As student
student3.firstName = "James"
student3.lastName = "Gosling"
student3.grade = 9.9
studentsList.Add(student3) ' Add student3 to students ArrayList
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Initialize a Comparator_Student in order to sort 'student' type
MACRO_DefineComparator(student)
Type studentComparator extends Comparator(student)
declare function compare(a as student, b as student) as Integer
End Type
function studentComparator.compare(a as student, b as student) as Integer
if a.grade = b.grade then
return 0
else
'return iif(a.grade > b.grade , 1, -1) ' Ascending
return iif(a.grade < b.grade , 1, -1) ' Descending
end if
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Sort the students list !
studentsList.sort(studentComparator)
print "Students ArrayList contains " & studentsList.size() & " elements"
print ""
print "Students by grade (Descending): "
for i as Integer = 0 to studentsList.size()-1
print "Student " & i & " = " & studentsList.get(i).firstName &" " & studentsList.get(i).lastName & " grade " & studentsList.get(i).grade
next i
- MySQL/MariaDB - Coming to v1.0.2
- ArrayList
- Comparator
- HashMap - Coming to v1.0.2
- Queue