java中的数组结构
Array struct in java
我正在学习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];
我可以在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;
}
}
但是我不知道如何在c++中把它变成结构体的最后一部分:
a [1000 + 10];
我的意思是,在Java中有一个大象类的对象数组就像在c++中有一个大象元素数组
有没有人能帮助我更好地理解它
Java中对象数组的实现方式与原语数组相同。语法应该是
Elephant[] elephants = new Elephant[1000+10];
这将初始化数组,但不会初始化元素。数组中的任何索引都将返回null,除非您执行以下操作:
elephants[0] = new Elephant();
这应该是你正在寻找的:
Elephant [] array = new Elephant[1010];
相关文章:
- C++将文本文件中的数据读取到结构数组中
- JSON转换为nlohmann JSON-lib中的结构数组
- 需要帮助将结构数组传递给函数
- 在 64 位边界上对齐C++结构数组?
- 带有枚举方向/类型的气泡排序结构数组
- 在C++中访问结构数组中的数据
- 从指向结构数组的指针中提取元素
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何在函数中显示结构数组
- 包含作为成员C++函数的结构数组
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- std::transform 将一个结构数组复制到另一个结构数组
- 将非托管结构数组封送到托管数组
- 显示结构数组
- 结构数组中所有类似字段的总和
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何将字符串字符与结构数组进行比较?
- C++结构数组