Java ArrayList如何添加类

Java ArrayList如何添加类,java,arrays,arraylist,Java,Arrays,Arraylist,当我试图在Java中创建ArrayList时遇到了一个问题,但更具体地说,当我试图add()到它时。我在people.add(joe)上发现语法错误行 Error: misplaced construct: VariableDeclaratorId expected after this token. at people.add(joe); ^ 我的理解是,就我的目的而言,ArrayList比数组更好,所以我的问题是,是这样吗?如果不是,我的语法哪里

当我试图在Java中创建
ArrayList
时遇到了一个问题,但更具体地说,当我试图
add()
到它时。我在
people.add(joe)上发现语法错误

Error: misplaced construct: VariableDeclaratorId expected after this token.
    at people.add(joe);
                  ^
我的理解是,就我的目的而言,
ArrayList
比数组更好,所以我的问题是,是这样吗?如果不是,我的语法哪里出了问题

这是我的密码

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);
}
import java.util.ArrayList;
公共阶层人士{
静态字符串名;
静态双年龄;
静态双高;
静态双重;
人员(字符串名称、双年龄、双身高、双体重){
Person.name=姓名;
Person.age=年龄;
Person.height=身高;
Person.weight=体重;
}
人物乔=新人(“乔”,30、70、180);
ArrayList people=新建ArrayList();
人物。加上(乔);
}
为什么这些变量被定义为静态变量

看起来你是在个人课上做的。在类中这样做是可以的(可以这样做),但是如果您正在创建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)
   }
}
import java.util.ArrayList;
公共阶层人士
{//不要在这里使用static,除非您需要所有人
//对象具有相同的数据
字符串名;
双倍年龄;
双高;
双倍重量;
公众人物(姓名、双倍年龄、双倍身高、双倍体重)
{
this.name=name;//必须引用具有
this.age=age;//与构造函数参数同名
this.height=height;//带有“this”关键字。无法使用
this.weight=weight;//带有非静态变量的Classname.variable
}
}
公共另一类
{
公共方法()
{
人物乔=新人(“乔”,30、70、180);
ArrayList people=新建ArrayList();
人物。加上(乔);
人物史蒂夫=新人(“史蒂夫”,28、70、170);
people.add(steve);//现在steve和Joe是两个独立的对象
//它们有自己的实例变量
//(非静态)
}
}

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

 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);
     }
 }
公共类人物{
公共静态void main(字符串[]args){
人物乔=新人(“乔”,30、70、180);
ArrayList people=新建ArrayList();
人物。加上(乔);
}
}

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

import java.util.ArrayList;
公共阶层人士
{
静态字符串名;
静态双年龄;
静态双高;
静态双重;
人员(字符串名称、双年龄、双身高、双体重)
{
Person.name=姓名;
Person.age=年龄;
Person.height=身高;
Person.weight=体重;
}
公共静态void main(字符串参数[])
{
人物乔=新人(“乔”,30、70、180);
ArrayList people=新建ArrayList();
人物。加上(乔);
}
}

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);
}
公共类人物{
静态字符串名;
静态双年龄;
静态双高;
静态双重;
人员(字符串名称、双年龄、双身高、双体重){
Person.name=姓名;
Person.age=年龄;
Person.height=身高;
Person.weight=体重;
}
}
公共静态void main(字符串[]args){
人物乔=新人(“乔”,30、70、180);
ArrayList people=新建ArrayList();
人物。加上(乔);
}

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

首先,您的
ArrayList
变量实际上应该位于实例级别,或者
static
。声明如下:

private ArrayList<Person> people;
第三,您需要调用它。您可以通过以下方式完成此操作:

Person.addPerson(new Person("Joe", 30, 70, 180));
main()
的主体中,或与程序执行相关的地方。

在周围放上花括号 人物。加上(乔); 代码将编译:

{people.add(joe);}


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

您需要在块中添加内容(方法、构造函数、初始值设定项等)。顺便说一句,您误用了static=您的所有人都将共享相同的姓名、年龄等。您希望删除static关键字,并将构造函数中的“Person.name=”替换为“this.name=”。感谢您提供的所有帮助。它现在在一个方法中工作。它在哪个类中完成并不重要。事实上,这是在方法或其他代码块之外进行的。@BeauGrantham:这一点很好,我理解,我只是认为做这样的事情没有多大意义。
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));