尝试在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);
}
答案 0 :(得分:5)
static String name;
static double age;
static double height;
static double weight;
为什么这些变量定义为静态?
看起来你在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)
}
}
答案 1 :(得分:0)
将该代码放在主方法中:
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);
}
}
答案 2 :(得分: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);
}
}
答案 3 :(得分:0)
ArrayList add操作应该在方法块中完成(可能在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);
}
答案 4 :(得分:0)
将代码(在构造函数之后)放在方法中,然后在其他地方调用该方法。您的字段不得为此目的而静态,因为所有实例都将共享它。
答案 5 :(得分: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()
的正文中,或与您的程序执行相关的某个地方。
答案 6 :(得分:0)
把花括号括起来 people.add(JOE); 代码将编译:
{people.add(joe);}
为什么呢?在初始化块上留下练习。