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

解决方案 »

  1.   

    struct[] st = new struct[100]
      

  2.   

    structer 是一个具体的对象,而不是一种类型,不能用对象声明对象
    不能
    structer[] st = new struct[100];应该这样用
    struct[] st = new struct[100];
      

  3.   

    ^_^ 改成这样就可以了!
    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;
    }
      

  4.   

    我要定义100个这样的数组成员的话,岂不是要new Struct()100次
    如:Struct[] st = new Struct[]{
                  new Struct(),
                  new Struct(),
                           new Struct(),
                                .
                                .
                                .
                           new Struct(),
               };
      

  5.   

    Struct[] st = new Struct[100];
              for(int i=0;i<st.length;i++){
               st[i]=new Struct();
              }
      

  6.   

    或者直接:
    Struct[] st = new Struct[100];
    Arrays.fill(st,new Struct());
    不过这样填充的就是相同的对象! 最后的结果就成了 99了!