Работал с Kotlin в Visual Studio и сталкивался с ошибкой : неразрешенная ссылка: при создании объекта класса SortingCode. Кажется, код работает нормально в IntelliJ . приветствуется любой указатель в правильном направлении.
ниже приведены два класса kotlin, которые использовались в проекте kotlin
github link
class SearchingAlgorithm {
private var sortingCode = sorting.kotlinCode.SortingCode() // TODO this line causing some issues for it to run in VScode
fun run() {
var numToSearch: Int = Random().nextInt(7) //2
var arrayToSearchIn = sortingCode.getSortedArray().clone()
var result = binarySearch(arrayToSearchIn, 0, arrayToSearchIn.size - 1, numToSearch)
if (!result.hasFoundNum) {
println("could not find $numToSearch in ${arrayToSearchIn.contentToString()}")
} else {
println("found $numToSearch at ${result.numberFoundAt} in ${arrayToSearchIn.contentToString()}")
}
}
private fun binarySearch(array: IntArray, low: Int, high: Int, target: Int): ResultAndIndex {
if (low > high) {
return ResultAndIndex(false, -1)
} else {
var midpoint = low + (high - low) / 2
if (target == array[midpoint]) {
return ResultAndIndex(true, midpoint)
} else if (target > array[midpoint]) {
return binarySearch(array, low, midpoint - 1, target)
} else {
return binarySearch(array, midpoint + 1, high, target)
}
}
}
/* Supporting classes */
class ResultAndIndex {
var hasFoundNum: Boolean = false
var numberFoundAt: Int = -1
constructor(hasFound: Boolean, atIndex: Int) {
hasFoundNum = hasFound
numberFoundAt = atIndex
}
}
}
fun main() {
var searchingAlgorithm = SearchingAlgorithm()
searchingAlgorithm.run()
}
import java.util.*
public class SortingCode {
private val arrays: Array<IntArray> = arrayOf(
intArrayOf(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6),
intArrayOf(7, 6, 5, 9, 8, 2, 4, 1),
intArrayOf(9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6, 9, 4, 2, 4, 5, 6),
intArrayOf(11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62, 11, 12, 63, 74, 51, 62),
intArrayOf(3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6, 3, 2, 1, 4, 5, 6),
intArrayOf(7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6, 7, 2, 8, 10, 5, 6)
)
var selectionLimit = 5
public fun run() {
var selection = -1
println("""
Enter 1 for Insertion Sort
Enter 2 for Selection Sort
Enter 3 for Merge Sort
Enter 4 for Progressive insertion sort
Enter 5 for Recursive insertion sort
Enter 9 to Exit
Please enter your choice ->
""")
while (selection != 9) {
val inputArray = arrays[Random().nextInt(arrays.size)].clone()
var sortedArray = inputArray
val (input) = readLine().toString().split(' ')
selection = input.toInt()
println("Input array to Sort-> " + inputArray.contentToString())
var currentTime = System.nanoTime()
when (selection) {
5 -> {
sortedArray = (insertionRecursiveSort(LinkedList<Int>(inputArray.toList()))).toIntArray()
}
9 -> {
println("Entered 9 to Exit")
return
}
else -> println("Please enter number from 1 to $selectionLimit to perform sorting")
}
println("Time took -> " + (System.nanoTime() - currentTime))
println("Sorted input array -> " + sortedArray.contentToString())
}
}
fun insertionRecursiveSort(array: LinkedList<Int>): LinkedList<Int> {
if (array.size == 2) {
val valueToInsert = array.removeFirst()
if (array[0] > valueToInsert) {
array.addLast(valueToInsert)
} else {
array.addFirst(valueToInsert)
}
return array
} else {
val valueToInsert: Int = array.removeFirst()
insertionRecursiveSort(array)
var hasValueAdded = false
for ((index, value) in array.withIndex()) {
if (value < valueToInsert) {
array.add(index, valueToInsert)
hasValueAdded = true;
break
}
}
if (!hasValueAdded) {
array.addLast(valueToInsert)
}
return array
}
}
fun getSortedArray(): IntArray {
var unsortedArray = arrays[Random().nextInt(arrays.size)].clone()
return (insertionRecursiveSort(LinkedList(unsortedArray.toList()))).toIntArray()
}
}
fun main() {
var sortingCode = SortingCode()
sortingCode.run()
}