English 中文(简体)
Java: getting a value from an array from a defined location
原标题:

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/api/java/lang/reflect/Array.html but my code still isn t compiling.

here is my method:

public class ConvexPolygon implements Shape
{
    java.awt.Point[] vertices;

    public ConvexPolygon(java.awt.Point[] vertices) 
    {
        this.vertices = vertices;
        this.color = color;
        this.filled = filled;
    }

java.awt.Point getVertex(int index)
{  
    Point vertex;
    vertex =  get(Point vertices, int index);  
}

I have numbers in an array representing Points. The value index is going to be the location of the array verities. What can I do to make this work? Thanks !

最佳回答

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];
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签