Java中的匿名数组 [英] Anonymous arrays in Java

查看:74
本文介绍了Java中的匿名数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与数组快捷方式相比​​,匿名数组的用途是什么?在什么情况下使用匿名数组?

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中的匿名数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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