与数组快捷方式相比,匿名数组的用途是什么?在什么情况下使用匿名数组?
What is the utility of anonymous arrays compared to arrays shortcut? In what scenario anonymous arrays are used?
推荐答案如果您的意思是匿名数组(如匿名类),则在不使用名称的情况下同时声明和实例化它.下面的示例显示了何时使用它.
If you mean anonymous arrays like anonymous class where it was declared and instantiated at the same time without a name. The example below shows when you would use it.
例如,您有一个方法希望将 int 的数组作为参数列表中的参数.
For example, you have a method which expects an array of int as the argument in the parameter list.
public void fillPolygon(int[] xPoints, int[] yPoints, nPoints)要调用此方法:
int[] xPoints = {1,2,3}; int[] yPoints = {4,5,6}; g.fillPolygon(xPoints, yPoints, 3);您也可以将其写为:
g.fillPolygon(new int[]{1,2,3}, new int[]{4,5,6}, 3);在哪种情况下使用匿名数组?
In what scenario anonymous arrays are used?
A:,因为您不保留数组的引用(名称),所以仅在一次一次使用它.
A: Use it when you are only going to use it once because you do not keep a reference (name) for the arrays.
问:那么使用匿名数组"有什么好处?
Q: So what is the benefit of using "anonymous array"?
A :它使您的代码简洁明了.
A: It keeps your codes concise.
更多推荐
Java中的匿名数组
发布评论