Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将项作为数组添加到类中_Java - Fatal编程技术网

Java将项作为数组添加到类中

Java将项作为数组添加到类中,java,Java,我有一个简单的下面的类结构,我想添加任何项目到它 public class Person implements Serializable { private String name; private String mobile; public Person(String n, String e) { name = n; mobile = e; } public String getName() { return name; } public Strin

我有一个简单的下面的类结构,我想添加任何项目到它

public class Person implements Serializable {
    private String name;
    private String mobile;

    public Person(String n, String e) { name = n; mobile = e; }

    public String getName() { return name; }
    public String getMobile() { return mobile; }

    @Override
    public String toString() { return name; }
}
我想添加任何类似以下内容的项目:

    people = new Person[]{
            new Person("Hi"   , " programmer"),
            new Person("Hello", " world")
    };
我的代码是这个,我想通过
while()
向其中添加项目我的代码不正确

people =  new Person[]{};
while (phones.moveToNext())
{
        people =  new Person("Hi"   , " programmer");
        people =  new Person("Hello", " world")
}

您没有定义要推入数组的元素数。此外,您甚至没有将这些元素添加到数组中。你应该这样做:

int i =0;
people =  new Person[1000];// you need to define how many elements you need here or go for list
while (phones.moveToNext())
{
    people[i++] =  new Person("Hi"   , " programmer");
    people[i++] =  new Person("Hello", " world")
}

您试图将person对象放入数组中的源代码中有错误,所以它会给您带来编译错误 要解决此问题,首先获取Person类型的列表,并将其转换为数组,然后在数组上执行业务逻辑。最好使用列表而不是数组

           List<Person> personlst = new ArrayList<Person>();
            while (phones.moveToNext())
            {
                personlst.add(new Person("Hi"   , " programmer"));
                personlst.add(new Person("Hello", " world"));
            }
            Object[]arryPer = personlst.toArray();
            Person[]people = new Person[arryPer.length];

        for (int j = 0; j < arryPer.length; j++) {
            people[j] = (Person) arryPer[j];
        }
List personlist=new ArrayList();
while(phones.moveToNext())
{
添加(新的人(“嗨”,“程序员”));
添加(新的人(“你好”,“世界”));
}
Object[]arryPer=personlst.toArray();
Person[]people=新人[arryPer.length];
对于(int j=0;j

上面的块代码为您提供了一个类型为people的数组

首先定义您的人的大小

人员[]人员=新人员[10]

然后进行迭代

for(int i = 0; i < 0; i++){
            people[i] = new Person("Hi"   , " programmer");
}
for(int i=0;i<0;i++){
人[我]=新人(“嗨”,“程序员”);
}

首先将所有元素放入列表中,然后形成数组:

List<Person> list = new ArrayList<Person>();
    while (phones.moveToNext()) {
        list.add(new Person("Hi", " programmer"));
        list.add(new Person("Hello", " world"));
    }
    Person[]  persons = new Person[list.size()];
    list.toArray(persons);
}
List List=new ArrayList();
while(phones.moveToNext()){
添加(新的人(“嗨”,“程序员”));
添加(新的人(“你好”,“世界”));
}
Person[]persons=新的Person[list.size()];
名单。待命人(人);
}
试试Arrays.asList

注意-它只适用于少量元素,因为数组通常使用连续内存


正如人们在上面所说的,从空间的角度来看,这个列表会更好。

你们有什么问题?什么是
phones.moveToNext()
?您正在将Person对象引用分配给阵列为什么不使用列表?因为你不知道你会有多少人…看起来你想要一个自动扩展或可调整大小的阵列。那是不存在的。您可能需要的是ArrayList。但是你必须首先理解数组。谢谢,先生。我得到以下错误:
致命异常:main java.lang.ClassCastException:java.lang.Object[]无法强制转换为ir.tsms.Person[]
对于此行
Person[]Person=(Person[])arryPer
@和Bee:它有一些强制转换问题。请检查已编辑的一个