2

我在尝试ArrayList用 Java 创建一个时遇到了一个问题,但更具体地说是在尝试创建add()它时。我在线收到语法错误people.add(joe); ...

Error: misplaced construct: VariableDeclaratorId expected after this token.
    at people.add(joe);
                  ^

我的理解是,ArrayList就我的目的而言,an 比数组更好,所以我的问题是,是这样吗?如果不是,我的语法在哪里出错?

这是我的代码...

import java.util.ArrayList;

public class Person {
    static String name;
    static double age;
    static double height;
    static double weight;

    Person(String name, double age, double height, double weight){
        Person.name = name;
        Person.age = age;
        Person.height = height;
        Person.weight = weight;
    }

    Person joe = new Person("Joe", 30, 70, 180);
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(joe);
}
4

7 回答 7

7
static String name;      
static double age;
static double height;
static double weight;

为什么将这些变量定义为static

看起来您正在 Person 类中执行此操作。在课堂上这样做是可以的(可以做到),但如果您正在创建 Person 对象的 ArrayList,则没有多大意义。

这里的要点是,这必须在实际的方法或构造函数或其他东西(实际的代码块)中完成。同样,我不完全确定 Person 类型的 ArrayList 在 Person 类中会有多大用处。

import java.util.ArrayList;

public class Person 
{                   // Don't use static here unless you want all of your Person 
                    // objects to have the same data
   String name;
   double age;
   double height;
   double weight;

   public Person(String name, double age, double height, double weight)
   {
      this.name = name;       // Must refer to instance variables that have
      this.age = age;         // the same name as constructor parameters
      this.height = height;    // with the "this" keyword. Can't use 
      this.weight = weight;    // Classname.variable with non-static variables
   }

}

public AnotherClass 
{
   public void someMethod()
   {
      Person joe = new Person("Joe", 30, 70, 180);
      ArrayList<Person> people = new ArrayList<Person>();
      people.add(joe);
      Person steve = new Person("Steve", 28, 70, 170);
      people.add(steve);            // Now Steve and Joe are two separate objects 
                                    // that have their own instance variables
                                    // (non-static)
   }
}
于 2012-06-20T00:35:54.847 回答
0

将该代码放在 main 方法中,例如:

 public class Person {

     public static void main(String[] args ) {
         Person joe = new Person("Joe", 30, 70, 180);
         ArrayList<Person> people = new ArrayList<Person>();
         people.add(joe);
     }
 }
于 2012-06-20T00:36:19.297 回答
0

将这些写在某个方法或块中。
像::

import java.util.ArrayList;

public class Person 
{
   static String name;
   static double age;
   static double height;
   static double weight;

  Person(String name, double age, double height, double weight)
  {
    Person.name = name;
    Person.age = age;
    Person.height = height;
    Person.weight = weight;
  }

  public static void main(String args[])
  {
    Person joe = new Person("Joe", 30, 70, 180);
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(joe);
  }
}
于 2012-06-20T00:36:28.680 回答
0

正如其他人建议的那样,ArrayList 添加操作应该在方法块(可能在 main)中完成。

public class Person  {
  static String name;
  static double age;
  static double height;
  static double weight;

  Person(String name, double age, double height, double weight) {
    Person.name = name;
    Person.age = age;
    Person.height = height;
    Person.weight = weight;
  }
}

public static void main(String[] args) {
  Person joe = new Person("Joe", 30, 70, 180);
  ArrayList<Person> people = new ArrayList<Person>();
  people.add(joe);
}
于 2012-06-20T00:36:42.660 回答
0

将您的代码(在构造函数之后)放在一个方法中,然后在其他地方调用该方法。为此,您的字段不能是静态的,因为所有实例都将共享它。

于 2012-06-20T00:36:44.077 回答
0

首先,您的ArrayList变量确实应该在实例级别,或者static. 像这样声明它:

private ArrayList<Person> people;

或者:

static ArrayList<Person> people;

其次,您需要某种功能来执行操作。

public static void addPerson(Person p) {
    people.add(p);
}

第三,您需要调用它。您可以通过以下方式做到这一点:

Person.addPerson(new Person("Joe", 30, 70, 180));

在 的主体中main(),或与您的程序执行相关的某个地方。

于 2012-06-20T00:37:11.253 回答
0

在 people.add(joe); 周围放置花括号 并且代码将编译:

{people.add(joe);}

为什么?作为初始化块的练习。

于 2012-06-20T00:45:19.163 回答