Java ArrayList如何添加类 [英] Java ArrayList how to add classes

查看:221
本文介绍了Java ArrayList如何添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Java中创建 ArrayList 时遇到问题,但更具体地说,在尝试 add()到它。我在 people.add(joe); 行中得到语法错误...

I'm running into a problem when trying to create an ArrayList in Java, but more specifically when trying to add() to it. I get syntax errors on the people.add(joe); line...

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

我的理解是 ArrayList 会比数组更好,所以我的问题是,是不是这样,我的语法出错了?

It's my understanding that an ArrayList would be better than an array for my purposes, so my question is, is that the case and if not, where am I going wrong with my syntax?

这是我的代码......

This is my code...

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


推荐答案

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

为什么这些变量定义为静态

看起来你在Person类中这样做了。在类中执行它是可以的(可以完成),但是如果要创建Person对象的ArrayList则没有多大意义。

It looks like you are doing it in the Person class. Doing it in the class is OK(it can be done), but doesn't make much sense if you are creating an ArrayList of Person objects.

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

The main point here is that this must be done within an actual method or constructor or something (an actual code block). Again, I am not entirely sure how useful an ArrayList of type Person would be inside of a Person class.

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)
   }
}

这篇关于Java ArrayList如何添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆