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

前端 未结 3 493
死守一世寂寞
死守一世寂寞 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.

    0 讨论(0)
  • 2020-12-12 08:40

    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 :)

    0 讨论(0)
  • 2020-12-12 08:40

    Hope it works!

    java.awt.Point getVertex(int index)
    {  
        return vertices[index];
    }
    
    0 讨论(0)
提交回复
热议问题