Android kotlin,从arrayObject创建arraylist

Android kotlin,从arrayObject创建arraylist,android,arrays,arraylist,kotlin,Android,Arrays,Arraylist,Kotlin,尝试使用下面代码中的Arraylist从我的对象生成Arraylist。 我的密码是:- var i = 1.0 var list: ArrayList<Any> = ArrayList() while (i <= n) { intPerMonth = P * R P = P - (e - intPerMonth) Log.e("TAG", "Month -> " + i.toInt())

尝试使用下面代码中的Arraylist从我的对象生成Arraylist。 我的密码是:-

       var i = 1.0
    var list: ArrayList<Any> = ArrayList()
    while (i <= n) {
        intPerMonth = P * R
        P = P - (e - intPerMonth)
        Log.e("TAG", "Month -> " + i.toInt())
        Log.e("TAG", "Interest per month -> " + Math.round(intPerMonth))
        Log.e("TAG", "Principal per month -> " + Math.round(e - intPerMonth))
        Log.e("TAG", "Balance Principal -> " + Math.round(P))
        Log.e("TAG", "***************************")

        list = arrayListOf(
            i.toInt(),
            Math.round(intPerMonth),
            Math.round(e - intPerMonth),
            Math.round(e - intPerMonth),
            Math.round(P)
        )
        i++
        Log.e("myArray", list.toString()) // this list.toString() is my output

    }

看起来你想要一个列表?尝试在每次迭代中创建一个内部列表,然后将其添加到列表中,如下所示:

var i = 1.0
var list: ArrayList<Any> = ArrayList()
while (i <= n) {
    intPerMonth = P * R
    P = P - (e - intPerMonth)
    Log.e("TAG", "Month -> " + i.toInt())
    Log.e("TAG", "Interest per month -> " + Math.round(intPerMonth))
    Log.e("TAG", "Principal per month -> " + Math.round(e - intPerMonth))
    Log.e("TAG", "Balance Principal -> " + Math.round(P))
    Log.e("TAG", "***************************")

    var innerList = arrayListOf( // create a different list here
        i.toInt(),
        Math.round(intPerMonth),
        Math.round(e - intPerMonth),
        Math.round(e - intPerMonth),
        Math.round(P)
    )
    list.add(innerList) // add the inner list to your list
    i++
}
Log.e("myArray", list.toString()) // this prints the new list of lists

尝试使用ArrayList类型这是kotlin bro@alesandrogiordano你能解释一下编码吗@AlesandGiordanoyes对不起,我将尝试在控制台中模拟它。。等等,好的,谢谢你@AlesandroGiordano
var i = 1.0
var list: ArrayList<Any> = ArrayList()
while (i <= n) {
    intPerMonth = P * R
    P = P - (e - intPerMonth)
    Log.e("TAG", "Month -> " + i.toInt())
    Log.e("TAG", "Interest per month -> " + Math.round(intPerMonth))
    Log.e("TAG", "Principal per month -> " + Math.round(e - intPerMonth))
    Log.e("TAG", "Balance Principal -> " + Math.round(P))
    Log.e("TAG", "***************************")

    var innerList = arrayListOf( // create a different list here
        i.toInt(),
        Math.round(intPerMonth),
        Math.round(e - intPerMonth),
        Math.round(e - intPerMonth),
        Math.round(P)
    )
    list.add(innerList) // add the inner list to your list
    i++
}
Log.e("myArray", list.toString()) // this prints the new list of lists