Java:从定义的位置获取数组中的值 [英] Java: getting a value from an array from a defined location

查看:117
本文介绍了Java:从定义的位置获取数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组数组,并希望从位置index中检索其中一个值。
我看过Java文档 http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Array.html ,但我的代码仍然没有编译。



这里是我的方法:

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

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

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

我在表示Points的数组中有数字。值索引将成为数组真实的位置。
我能做些什么来完成这项工作?谢谢!

解决方案

在Java中,数组索引由方括号表示。你可以像这样替换get(vertices,index)调用:

  vertex = vertices [index]; 

在查看代码时,看起来您来自定义全局get()功能用于这种操作。请注意,在Java中,没有全局函数。你创建的每个类都定义了它自己的函数,并且任何没有它之前的对象或类的函数调用都被假定为在本地类中定义。

因此, get(Point [],int)只有当你在这个类上定义该函数时才能工作:

  public Point get(Point [ ]顶点,int索引){
返回顶点[index];
}

或者在另一个类上静态地定义它,并且在类名前面调用:

  public class PointArrayHelper {

public static Point get(Point [] vertices,int index){
返回顶点[index];
}
}

PointArrayHelper.get(vertices,index);

现在,请注意,我认为您不应该这样做!我只是认为它可以帮助你更好地理解Java。


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.

这篇关于Java:从定义的位置获取数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆