Arrays 使用kotlin如何将数组附加到二维数组

Arrays 使用kotlin如何将数组附加到二维数组,arrays,kotlin,Arrays,Kotlin,我想获取坐标并将它们添加到一个数组中,我想从一个函数返回这个数组,但我不知道如何在kotlin中使用另一个数组将它们追加到一个空数组中 var temp:Array<Array<Int>> = arrayOf<Array<Int>>() var i:Int = 0 while (true){ // if you see another of t

我想获取坐标并将它们添加到一个数组中,我想从一个函数返回这个数组,但我不知道如何在kotlin中使用另一个数组将它们追加到一个空数组中

 var temp:Array<Array<Int>> = arrayOf<Array<Int>>()
                var i:Int = 0
                while (true){
                    // if you see another of the same type then break
                    if(currentPlayer == 1){
                        if(ystart-i < 0){ // if you would go out of bounds on the next it and the current one does not have an opposite then breakwith temp as nothing
                            temp = arrayOf<Array<Int>>()
                            break
                        }else if(touchnum[ystart-i][xstart] == 1){
                            break
                        }else{
                            val slice: IntArray = intArrayOf(xstart, ystart-i)
                            temp.plusElement(slice)

                        }

                    }else{

                    }
var-temp:Array=arrayOf()
变量i:Int=0
while(true){
//如果你看到另一个相同的类型,然后打破
如果(currentPlayer==1){
如果(ystart-i<0){//如果你想在下一个it上越界,而当前it没有相反的值,那么使用temp作为nothing进行中断
temp=arrayOf()
打破
}else if(touchnum[ystart-i][xstart]==1){
打破
}否则{
val切片:IntArray=intArrayOf(xstart,ystart-i)
温度脉冲(片)
}
}否则{
}

数组在JVM上的长度是固定的。一旦创建,就不能更改它们的长度。 动态增长数组的等效概念称为列表

在Kotlin中,它们由接口表示(当您需要只读引用时,可以使用
List
)。 您可以使用函数
mutableListOf()
(其中
T
是列表中元素的类型)创建
MutableList
的实例。然后使用
list.add(element)
添加元素

这是你的“主要”列表。现在让我们谈谈你在其中添加的元素

在Kotlin中,将元组(如坐标)表示为数组不是很惯用。对于2D坐标,已经有一种类型称为。但老实说,自己编写特定于域的类非常便宜,因此我鼓励您创建自己的类,例如:

数据类Point2D(val x:Int,val y:Int)
然后可以将此类的实例添加到列表中:

list.add(Point2D(xstart,ystart-i))

使用可变列表。数组用于希望元素数量永远不变的情况。