如何将arrayList的元素传递给可变函数 [英] How to pass elements of an arrayList to variadic function

查看:283
本文介绍了如何将arrayList的元素传递给可变函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个arrayList元素。我想将数组列表的元素作为变量函数的参数传递。



我的函数

  public SequenceEntityModifier(final IEntityModifier ... pEntityModifiers)



<

  ArrayList< IEntityModifier> arr = new ArrayList< IEntityModifier>(); 
arr.add(new MoveXModifier(1,50,120));
arr.add(new MoveXModifier(1,120,50));

我想将它传递给函数,就像我们单独传递它们一样。

  new SequenceEntityModifier(/ * arr here * /)元素; 

这样的东西可能吗?



提前感谢。

解决方案

>

  new SequenceEntityModifier(arr.toArray(new IEntityModifier [arr.size()])); 

这会将 ArrayList 复制到给定数组并返回它。所有vararg函数也可以为参数取数组,因此:

  public void doSomething(Object ... objs)

所有合法来电皆为:

  doSomething(); //空数组
doSomething(obj1); //一个元素
doSomething(obj1,obj2); //两个元素
doSomething(new Object [] {obj1,obj2}); //两个元素,但作为数组传递

一个警告: p>

涉及原始数组的Vararg调用不会按预期工作。例如:

  public static void doSomething(Object ... objs){
for(Object obj:objs) {
System.out.println(obj);
}
}

public static void main(String [] args){
int [] intArray = {1,2,3} $ b $ d doSomething(intArray);
}

可能希望这可以打印 1 2 3 相反,它会打印 [I @ 1242719c (默认 toString result for a int [] )。这是因为它最终用一个元素创建一个 Object [] ,这是我们的 int [] ,例如: / p>

  //基本上上面的代码执行的操作
Object [] objs = new Object [] {intArray};


double [] code> char [] 和其他原始数组类型。注意,这可以简单地通过将 intArray 的类型更改为 Integer [] 来修复。如果你使用现有的数组,这可能不简单,因为你不能直接把 int [] 转换为 Integer [] (请参阅此问题,我特别喜欢ArrayUtils.toObject 方法,来自 Apache Commons Lang )。


I've got an arrayList filled with elements. I would like to pass the elements of that array list as arguments to a variadic function.

My function

public SequenceEntityModifier(final IEntityModifier... pEntityModifiers)

My ArrayList

ArrayList<IEntityModifier> arr = new ArrayList<IEntityModifier>();
arr.add(new MoveXModifier(1, 50, 120));
arr.add(new MoveXModifier(1, 120, 50));

I'd like to pass it to the function as if I would pass them individually.

new SequenceEntityModifier( /* elements of arr here */ );

Is something like this possible?

Thanks in advance.

解决方案

Just do:

new SequenceEntityModifier(arr.toArray(new IEntityModifier[arr.size()]));

This copies the ArrayList to the given array and returns it. All vararg functions can also take arrays for the argument, so for:

public void doSomething(Object... objs)

All the legal calls are:

doSomething(); // Empty array
doSomething(obj1); // One element
doSomething(obj1, obj2); // Two elements
doSomething(new Object[] { obj1, obj2 }); // Two elements, but passed as array

One caveat:

Vararg calls involving primitive arrays don't work as you would expect. For example:

public static void doSomething(Object... objs) {
    for (Object obj : objs) {
        System.out.println(obj);
    }
}

public static void main(String[] args) {
    int[] intArray = {1, 2, 3};
    doSomething(intArray);
}

One might expect this to print 1, 2, and 3, on separate lines. Instead, it prints something like [I@1242719c (the default toString result for an int[]). This is because it's ultimately creating an Object[] with one element, which is our int[], e.g.:

// Basically what the code above was doing
Object[] objs = new Object[] { intArray };

Same goes for double[], char[], and other primitive array types. Note that this can be fixed simply by changing the type of intArray to Integer[]. This may not be simple if you're working with an existing array since you cannot cast an int[] directly to an Integer[] (see this question, I'm particularly fond of the ArrayUtils.toObject methods from Apache Commons Lang).

这篇关于如何将arrayList的元素传递给可变函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆