Why do I get a compilation warning here (var args method call in Java)

前端 未结 5 1619
失恋的感觉
失恋的感觉 2020-12-04 17:29

Source:

public class TestVarArgs {
  public void varArgsMethod(Object ... arr) {
     System.out.println(arr.getClass().getName());
     for(Object o : arr)          


        
5条回答
  •  一生所求
    2020-12-04 18:05

    One of the elegant ways to get rid off this warning: instead of tva.varArgsMethod(args) call tva.varArgsMethod(Arrays.stream(args).toArray()) explicitly showing that you pass multiple arguments. Another way to get rid off the warning is to use following call:

    tva.varArgsMethod(Arrays.asList(args).toArray())
    

    Anyway we have to convert the args to Array.

提交回复
热议问题