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

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

问题描述

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

错误:错位的构造:在此标记之后需要 VariableDeclaratorId.在 people.add(joe);^

我的理解是 ArrayList 对我来说比数组更好,所以我的问题是,是这种情况吗?如果不是,我的语法哪里出了问题?>

这是我的代码...

import java.util.ArrayList;公共类人{静态字符串名称;静态双龄;静态双高度;静态双重;人(字符串名称,双倍年龄,双倍身高,双倍体重){人名 = 姓名;Person.age = 年龄;Person.height = 身高;Person.weight = 体重;}Person joe = new Person("Joe", 30, 70, 180);ArrayList<人>people = new ArrayList();人.添加(乔);}

解决方案

static String name;静态双龄;静态双高度;静态双重;

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

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

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

import java.util.ArrayList;公开课人{//不要在这里使用静态,除非你想要你所有的 Person//拥有相同数据的对象字符串名称;双岁;双高;双倍重量;公众人物(字符串名称,双倍年龄,双倍身高,双倍体重){this.name = 名称;//必须引用具有this.age = 年龄;//与构造函数参数同名this.height = 高度;//使用this"关键字.不能用this.weight = 重量;//Classname.variable 带有非静态变量}}公共另一个班级{public void someMethod(){Person joe = new Person("Joe", 30, 70, 180);ArrayList<人>people = new ArrayList();人.添加(乔);Person steve = new Person("Steve", 28, 70, 170);人.添加(史蒂夫);//现在史蒂夫和乔是两个独立的对象//有自己的实例变量//(非静态)}}

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

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;

Why are these variables defined as static?

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.

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天全站免登陆