I have an object QuickSort that I am attempting to create 2 instances of. When I try to create 2 separate instances I can see that it is only using one instance because I have a count in the QuickSort class that is inaccurate. Kotlin does not use new in the syntax, so how would I go about this?
object QuickSort {
var count = 0;
quickSortOne(...){
...
count++
...
}
quickSortTwo(...){
...
count++
...
}
}
Here is how I am attempting to create my 2 instances.My goal is to have quickSort1 and quickSort2 be 2 separate instances.
var quickSort1 = QuickSort
quickSort1.quickSortOne(...)
var quickSort2 = QuickSort
quickSort2.quickSortTwo(...)
Attempted Solution: Converting QuickSort from an object to a class. This still results in the same instance being used as seen by the count of the second method including the first calls count.
class QuickSort {
var count = 0;
quickSortOne(...){
...
count++
...
}
quickSortTwo(...){
...
count++
...
}
}
...
var quickSortFirst = QuickSort()
printTest(quickSortFirst.quickSortFirst(arrayList, 0, arrayList.size - 1))
var quickSortLast = QuickSort()
printTest(quickSortLast.quickSortLast(arrayList, 0, arrayList.size - 1))
companion object
? It's purpose is: Note that, even though the members of companion objects look like static members in other languages, at runtime those are still instance members of real objects,... – Byrlecompanion object{}
and updated my code exactly as implemented above. It still is using the same QuickSort() instance. – FatherhoodMy goal is to have quickSort1 and quickSort2 be 2 separate instances.
Your attempted solution does just that. Full code would be helpful, there is a lot of suspicious looking bits. (Where didquickSortFirst
method suddenly come from? IsquickSortOne
a function? Both of them operate on the same array list, is that ok?) – Unionist