有没有办法在定义函数的返回类型时指定数组的长度?

huangapple go评论64阅读模式
英文:

Is there a way to specify the length of an array when defining a function's return type?

问题

如果我有一个返回数组的方法,是否有办法指定它返回的数组长度?类似于以下方式:

public int[2] getPoint() {
    return new int[] {0, 1};
}

这将有助于在子类中覆盖此方法时添加限制,并确保其他类正确实现此方法,因为它们将知道它必须返回长度为2的数组(或其他任何长度)。是否有任何方法可以实现这一点?

英文:

If I have a method that returns an array, is there a way to specify the length of the array that it returns? Something along the lines of:

public int[2] getPoint() {
    return new int[] {0, 1};
}

This would help add restrictions when overriding this method in a subclass, and make sure other classes implement this method correctly, because they will know that it must return an array of length 2 (or whatever else). Is there any way to do this?

答案1

得分: 4

短答案是:不可以。你不能使用数组来实现这个。

较长的答案是:如果你发现自己需要这样的情况,你真正需要的是一个包含两个字段的类。根据你的应用程序领域,它的名称可以不同,例如,对于图形应用程序,你可以创建一个带有x和y坐标的Point类。

public class Point {
  private final int x;
  private final int y;

  // 常规的构造函数、获取器、设置器等
}

或者,如果你不想为这个目的创建自己的类,你可以使用元组(tuple),比如来自Apache Commons库的Pair。

类是提供这种抽象的方法,使用类来描述你需要的数据结构不仅有助于完成任务,还使代码对你的代码读者更容易理解,包括未来的你。

英文:

Short answer is: no. You cannot do this with an array.

Longer answer is: if you find yourself in a situation that you need something like this, what your really need is a class with two fields. Based on the domain of your application it can be named differently, say for a graphics app you can have a Point class with x and y coordinates.

public class Point {
  private final int x;
  private final int y;

  // the usual constructor, getters, setters and stuff
}

Or, if you don't want to create your own class for such a purpose you can use a tuple, like e.g. Pair from Apache Commons lib.

Classes are the way to provide such abstractions, describing the data structure you need with classes not only helps you get the job done, but makes the code more understandable for readers of your code including future you 有没有办法在定义函数的返回类型时指定数组的长度?

答案2

得分: 2

No, length of array is not defined in type

指定返回的数组的长度

No, declaring the return type of a method as being an array does not include a length.

但是实际上,这并不重要。要达到类似的效果:

  • 返回一个不可修改的 List 而不是数组。
  • 调用代码可以简单地询问列表的大小。

return List.of("Bob", "Newhart");

当然,调用代码也可以询问数组的大小,就像询问列表一样。但使用不可修改的列表会锁定该大小。

Define a class

而不是通过修改数组或列表来隐含传达含义,定义一个类来明确表示您值的语义。

记录 (Records)

Java 16 的新特性 (在 14 中预览,在 15 中 预览),使这变得非常简单。

record Name(String givenName, String surname) {}

因此,您将返回这种类型的对象。


return new Name("Bob", "Newhart");

Point 示例

在您的示例中,定义一个名为 Point 的记录。

record Point(int x, int y) {}

实例化。


return new Point(7, 42);

访问数据。

System.out.println("x = " + myPoint.x());

x = 7

英文:

No, length of array is not defined in type

>specify the length of the array that it returns

No, declaring the return type of a method as being an array does not include a length.

But, effectively, that does not really matter. To get a similar effect:

  • Return a non-modifiable List rather than an array.
  • The calling code can simply ask the list for its size.

return List.of( "Bob" , "Newhart" ) ;

Of course the calling code can ask an array for its size as well as asking a list. But using a non-modifiable list locks in that size.

Define a class

Rather than hack an array or list to have certain values in certain slots to communicate meaning implicitly, define a class to represent the semantics of your values explicitly.

Records

The new Records feature of Java 16 (previewed in 14 & in 15), makes this utterly simple.

record Name ( String givenName, String surname  ) {}

So you would return an object of this type.

…
return new Name( "Bob" , "Newhart" ) ;

Point example

In your example, define a record named Point.

record Point( int x , int y ) {}

Instantiate.

…
return new Point( 7 , 42 ) ;

Access the data.

System.out.println( "x = " + myPoint.x() ) ;

>x = 7

答案3

得分: 0

在方法的返回类型中不能定义数组长度。正如其他人建议的,您可以通过定义一个具有一组属性的类来实现它。

英文:

No array length can't be defined in the return type of the method.As others suggest you can do it defining a class with set of properties.

huangapple
  • 本文由 发表于 2020年8月6日 13:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63277612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定