Java ArrayList如何添加类
我在尝试使用Java创建ArrayList
时遇到了问题,但更具体地说是在尝试add()
时。 我在people.add(joe);
上遇到语法错误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 people = new ArrayList(); people.add(joe); }
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 people = new ArrayList (); 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) } }
将该代码放在一个主方法中,如:
public class Person { public static void main(String[] args ) { Person joe = new Person("Joe", 30, 70, 180); ArrayList people = new ArrayList (); people.add(joe); } }
在一些方法或块中写下这些。
喜欢::
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 people = new ArrayList (); people.add(joe); } }
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 people = new ArrayList (); people.add(joe); }
将代码(在构造函数之后)放在方法中,然后在其他地方调用该方法。 您的字段不得为此目的而静态,因为所有实例都将共享它。
首先,您的ArrayList
变量确实应该在实例级别或static
。 声明如下:
private ArrayList people;
要么:
static ArrayList people;
其次,您需要某种function来执行操作。
public static void addPerson(Person p) { people.add(p); }
第三,你需要调用它。 你可以这样做:
Person.addPerson(new Person("Joe", 30, 70, 180));
在main()
的主体中,或者与程序执行相关的某个地方。
在people.add(joe)周围放置花括号; 并且代码将编译:
{people.add(joe);}
为什么? 在初始化块上留下练习。
- java ArrayList中最常见的n – 单词
- Java如何处理来自Object的Array 的“Unchecked cast”
- 找出对象列表是否包含具有指定字段值的内容?
- 从内存分配角度看ArrayList与LinkedList
- “线程中的exception”主“java.lang.IndexOutOfBoundsException:Index:0,Size:0”with ArrayList?
- JAVA ArrayList:如何知道它是否包含String数组?
- 集合emptyList / singleton / singletonList / List / Set toArray
- 按日期排序特殊字符串的数组列表
- 在JavaFX中使用ObservableList是否违反模型 – 视图 – 控制器分离?