Java中的匿名数组

编程入门 行业动态 更新时间:2024-10-25 14:27:22
本文介绍了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中的匿名数组

本文发布于:2023-10-18 13:23:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!