java中的数组结构

net*_*eot 1 c++ java struct

我正在学习Java,我正在将一些C++代码编写到java中,我正在关注这个网页http://uva.onlinejudge.org并尝试用Java来解决一些问题.现在我正在做这个问题http://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&page=show_problem&problem=1072,经过研究并弄清楚如何在纸上做到这一点我找到了这个网页,问题似乎很容易遵循:http: //tausiq.wordpress.com/2010/04/26/uva-10131/

但是现在,由于我是Java的新手,我想学习如何在Java中使用struct数组.我现在可以做类似结构的类:如果这是在C++中

struct elephant {
    int weight;
    int iq;
    int index;
} a [1000 + 10];
Run Code Online (Sandbox Code Playgroud)

我可以用Java做到这一点:

public class Elephant {
        private int _weight;
        private int _iq;
        private int _index;

        public Elephant(int weight, int iq, int index) {
            this._weight = weight;
            this._iq = iq;
            this._index = index;
        }

        public int getWeight() {
            return this._weight;
        }

        public int getIQ() {
            return this._iq;
        }

        public int getIndex() {
            return this._index;
        }

        public void setWeigth(int w) {
            this._weight = w;
        }

        public void setIQ(int iq) {
            this._iq = iq;
        }

        public void setIndex(int i) {
            this._iq = i;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将其转换为c ++中结构的最后一部分:

a [1000 + 10];
Run Code Online (Sandbox Code Playgroud)

我的意思是,在Java中有一个类Elephant对象的数组,比如在c ++中有一个元素数组

有人可以帮助我更好地理解它..

Chr*_*son 5

Java中的对象数组的完成方式与基元数组相同.这个的语法是

Elephant[] elephants = new Elephant[1000+10];
Run Code Online (Sandbox Code Playgroud)

这将初始化数组,但不会初始化元素.数组中的任何索引都将返回null,直到您执行以下操作:

elephants[0] = new Elephant();
Run Code Online (Sandbox Code Playgroud)