Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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 使用临时对象从数组创建ArrayList_Java_Arraylist - Fatal编程技术网

Java 使用临时对象从数组创建ArrayList

Java 使用临时对象从数组创建ArrayList,java,arraylist,Java,Arraylist,我知道有一种标准的方法可以向类中添加构造函数。但对于具有对象超类(没有参数构造函数)的类,我倾向于使用临时对象更简单。这种行为有什么不好的一面吗 import java.util.ArrayList; public class Main { public static void main(String[] args){ // data available in two separated arrays String[] dNa

我知道有一种标准的方法可以向类中添加构造函数。但对于具有对象超类(没有参数构造函数)的类,我倾向于使用临时对象更简单。这种行为有什么不好的一面吗

    import java.util.ArrayList;
    public class Main { 

      public static void main(String[] args){

        // data available in two separated arrays  
        String[] dName = {"Sam","Ben","Joye","Sarah","Tim","Lucy","Jack"} ;
        int[] dAge= {10,52,53,15,12,60,21};

        // Array list to put the data in 
        ArrayList<Person> personList =new ArrayList<Person>(7);

        for (int i=0;i<dName.length;i++){
            Person tempPerson = new Person();
            tempPerson.name= dName[i];
            tempPerson.age= dAge[i];
            personList.add(new Person());
            personList.set(i,tempPerson);
            tempPerson=null;    //removes the reference
        }

        for (int j=0 ; j<personList.size();j++){
            System.out.println(personList.get(j).name+" age is "+personList.get(j).age );
        }

      }  
    }

class Person{
    String name;
    int age;
}
不完全是


你也许可以使用java8流,但是为什么要让你的生活更艰难呢?它不会添加任何新的内容。你应该避免那些什么都不做的语句——优化就是这样

   for (int i=0;i<dName.length;i++){
        Person tempPerson = new Person();
        tempPerson.name= dName[i];
        tempPerson.age= dAge[i];
        personList.add(tempPerson);
    }
导致类似于

 personList.add(new Person().setName(dName[i]).setAge(dAge[i]));
再说一遍——两值构造函数可能是最简单的——超级类没有构造函数并不重要:

public Person(String aName, int aAge) {
   name = aName;
   age = aAge;
}
//You can have more than one constructor
public Person() {
}
然后

personList.add(new Person(dName[i], sAge[i]));

您应该为Person使用构造函数。然后,在for循环中只有一个调用:

personList.add(new Person(dName[i], dAge[i])

另外,在您的实现中,您要做两次必要的工作,因为您调用
personList.add(newperson())
,然后调用
personList.set(i,temPerson)
。如果您不想在Person类中使用构造函数,例如调用
personList.add(tempPerson)
就足够了。

为什么要先将
new Person()
添加到列表中,然后再将
tempPerson
?此外,无需删除引用-它在每个循环周期后都是“自由”的。首先,我不理解你的问题。是否要从这两个数组自动创建ArrayList?其次,for循环中的对象管理有点奇怪。为什么要将new Person()添加到ArrayList中,然后将tempPerson设置为null?是的,您的意思是,它将释放每个循环,因为它是再次构造的。我认为最好在再次更新之前销毁。pca,每个循环中都需要新的引用,否则整个丢失的值将是相同的值(最后的数组值),因为列表将具有相同的对象引用。不是真的。创建对象,然后将其添加到列表中。此时,tempPerson和列表将引用该对象。当您执行tempPerson=newPerson()时;数组仍将保留上一个对象。就像Jan的回答中一样。谢谢,我只是认为我需要在再次初始化之前销毁对象引用。我第一次尝试直接添加,但遇到了麻烦(看起来好像其他地方出了问题),所以我想我需要使用add(新建),然后我可以设置值。再次感谢,现在它很完美是的,当然构造函数会更方便,但有时使用临时对象会更容易。是的。添加(和.Set(以后不用了,谢谢
personList.add(new Person(dName[i], sAge[i]));
personList.add(new Person(dName[i], dAge[i])