类struct,structer为该类的对象,我想定义一个struct型的数组,不知道怎么做??代码如下,但有错:public class StructTest {
int a;
char b; public void getStruct(struct st[]){
System.out.print(st[1].a);
System.out.print("\n");
System.out.print(st[1].b);
} public static void main(String[] args) throws Exception
{
StructTest test = new StructTest();
struct structer = new struct() ;
structer[] st = new struct[100];
int i;
for(i=0;i<10;i++){
st[i].setStruct(i,'Y');
} test.getStruct(st[i]);
}
}class struct{
public void setStruct(int a, char b){
this.a = a;
this.b = b;
} int a;
char b;
}
int a;
char b; public void getStruct(struct st[]){
System.out.print(st[1].a);
System.out.print("\n");
System.out.print(st[1].b);
} public static void main(String[] args) throws Exception
{
StructTest test = new StructTest();
struct structer = new struct() ;
structer[] st = new struct[100];
int i;
for(i=0;i<10;i++){
st[i].setStruct(i,'Y');
} test.getStruct(st[i]);
}
}class struct{
public void setStruct(int a, char b){
this.a = a;
this.b = b;
} int a;
char b;
}
不能
structer[] st = new struct[100];应该这样用
struct[] st = new struct[100];
public class StructTest {
int a;
char b; public void getStruct(Struct st[]){
System.out.print(st[1].a);
System.out.print("\n");
System.out.print(st[1].b);
} public static void main(String[] args) throws Exception
{
StructTest test = new StructTest();
Struct[] st = new Struct[]{
new Struct(),
new Struct(),
};
for(int i=0;i<st.length;i++){
st[i].setStruct(i,'Y');
}
test.getStruct(st);
}
}class Struct{
public void setStruct(int a, char b){
this.a = a;
this.b = b;
} int a;
char b;
}
如:Struct[] st = new Struct[]{
new Struct(),
new Struct(),
new Struct(),
.
.
.
new Struct(),
};
for(int i=0;i<st.length;i++){
st[i]=new Struct();
}
Struct[] st = new Struct[100];
Arrays.fill(st,new Struct());
不过这样填充的就是相同的对象! 最后的结果就成了 99了!