Kotlin passes variable length arguments to Java variable length instance code

  • 2021-01-06 00:33:07
  • OfStack

This paper mainly studies the method of Kotlin passing variable length parameter to Java variable parameter. The specific implementation code is as follows.

Define Java variable parameter methods


package com.tcl.john.studymvvm.utils;

/**
 *  call Java Method's utility class 
 * Created by ZhangJun on 2017/10/25.
 */

public class CallJavaUtils {

  public static int addNumbers(String name, int... args) {
    int result = 0;
    for (int i = 0; i < args.length; i++) {
      result += args[i];
    }
    return result;
  }
}

Kotlin passes the variable length argument and calls the Java method above


// test Kotlin Pass variable length arguments to Java Variable parameter method 
var numbers:IntArray = intArrayOf(1, 2, 3, 4, 5)
CallJavaUtils.addNumbers("add", *numbers)

conclusion

That is all of the code in this article on the example of Kotlin passing variable length arguments to Java variable arguments. I hope it will help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: