Java: getting a value from an array from a defined location

余生颓废 提交于 2019-11-28 14:36:00

In Java, array indexes are denoted by the square brackets. You can replace your get(vertices, index) call like so:

  vertex = vertices[index];

In looking at your code, it appears you are coming from a language that defines a global get() function for such operations. Be aware that, in Java, there are no global functions. Each class you create defines its own functions, and any function call without an object or class preceding it is assumed to be defined in the local class.

So, your call to get(Point[], int) could work only if you define that function on this class:

  public Point get(Point[] vertices, int index) {
     return vertices[index];
  }

Or define it statically on another class and precede the call with the class name:

public class PointArrayHelper {

  public static Point get(Point[] vertices, int index) {
    return vertices[index];
  }
}

PointArrayHelper.get(vertices, index);

Now, be warned that I don't think you should do either of these! I just thought it might help you understand Java a little better.

I think you're just looking for:

 Point vertex = vertices[index];

At least - if you're not looking for that, please expand on what the difference is between using the array index and what you do want :)

Hope it works!

java.awt.Point getVertex(int index)
{  
    return vertices[index];
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!