How to initialize an array in Kotlin with values?

前端 未结 20 2109
谎友^
谎友^ 2020-12-22 21:03

In Java an array can be initialized such as:

int numbers[] = new int[] {10, 20, 30, 40, 50}

How does Kotlin\'s array initialization look li

20条回答
  •  再見小時候
    2020-12-22 21:22

    In my case I need to initialise my drawer items. I fill data by below code.

        val iconsArr : IntArray = resources.getIntArray(R.array.navigation_drawer_items_icon)
        val names : Array = resources.getStringArray(R.array.navigation_drawer_items_name)
    
    
        // Use lambda function to add data in my custom model class i.e. DrawerItem
        val drawerItems = Array(iconsArr.size, init = 
                             { index -> DrawerItem(iconsArr[index], names[index])})
        Log.d(LOGGER_TAG, "Number of items in drawer is: "+ drawerItems.size)
    

    Custom Model class-

    class DrawerItem(var icon: Int, var name: String) {
    
    }
    

提交回复
热议问题