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

前端 未结 3 495
死守一世寂寞
死守一世寂寞 2020-12-12 08:17

I have an array of numbers and would like to retrieve one of the values from location \"index\". I\'ve looked at the Java documentation http://java.sun.com/j2se/1.5.0/docs/a

3条回答
  •  再見小時候
    2020-12-12 08:26

    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.

提交回复
热议问题