0

my Data.builder() method putStringArray() takes parameter as key : String and value : Array

how do I convert my ArrayList into Array?

fun createInputData(dataItems : ArrayList<String>) : Data
    {
       
        return Data.Builder()
                .putStringArray("dataItems",dataItems)
                .build()
    }

the above code doesn't work.

2
  • and how do i convert it back to an arraylist of string? Commented Jun 25, 2020 at 9:33
  • you can use .toList() extention method for Array Commented Jun 25, 2020 at 9:46

2 Answers 2

2

dataItems.toTypedArray() should do the trick

Sign up to request clarification or add additional context in comments.

Comments

0

Please check the below method:-

Convert To Array:

fun main(args: Array<String>) {

    // an arraylist of vowels
    val vowels_list: List<String> = listOf("a", "e", "i", "o", "u")
    
    // converting arraylist to array
    val vowels_array: Array<String> = vowels_list.toTypedArray()
    
    // printing elements of the array 
    vowels_array.forEach { System.out.print(it) }
}

Convert To List:

fun main(args: Array<String>) {

    // vowels array
    val vowels_array: Array<String> = arrayOf("a", "e", "i", "o", "u")
    
    // converting array to array list
    val vowels_list: List<String> = vowels_array.toList()
    
    // printing elements of the array list
    vowels_list.forEach { System.out.print(it) }
}

1 Comment

and how do i convert it back to an arraylist of string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.