将对象克隆到列表中,并为每个项目更改了多个值

时间:2018-02-22 11:41:17

标签: java list reference

我试图让我的代码将名为OutputData的{​​{1}}对象克隆到列表中,为我正在制作的每个克隆更改一个int值。这是我提出的代码:

primitive

我的问题是,当我检查结果时,只有最后一个值被保存并重复每个克隆对象,所以我猜测我是以某种方式将整个引用复制到对象。我该怎么做才能正确,所以每个对象都包含正确的信息?

这是方法的输出:

public void fixAndSave(){
    for(int i = 1; i <= NUM_EXEC; i++){
        InputData custom = new InputData(primitive);
        for (Section s : custom.getSections()){
            if (s.getId() != 0 )
                s.setBloques(i*s.getId());
        }
        collection.add(custom);
        System.out.println("GUARDANDO DATA: " + custom.getSections().toString());

    }
    collection.forEach((InputData d) -> {
        System.out.println("DATA GUARDADO: " + d.getSections().toString());
    });
}

2 个答案:

答案 0 :(得分:1)

您需要执行深层复制,同时在调用getSections()时考虑InputData custom = new InputData(primitive); 返回的字段:

public InputData(OutputData outputData){
     // ...
     List<Section> copiedSections = new ArrayList<>();
     for (Section section : outputData.getSections()){
           Section copiedSection = new Section(section); // copy constructor
           copiedSections.add(copiedSection);
     }
     this.sections = copiedSections;    
}

它可能看起来像:

if($count <= 0 ) // IF TABLE DOES NOT EXIST -> CREATE AND INSERT DATA
{
    $CREATE_TABLE= "CREATE TABLE $TABLE_NAME LIKE student; INSERT $TABLE_NAME SELECT * FROM student;";
    $created = $connect->exec($CREATE_TABLE);

    if($created!=FALSE)
    {
        $SQL = "INSERT INTO $TABLE_NAME (name, roll_number, father_name, dob, gender, address, email, phone, department, program, semester, section) VALUES(:name, :roll_number, :father_name, :dob, :gender, :address, :email, :phone, :department, :program, :semester, :section)";
        $pdo_statement = $connect->prepare($SQL);

        $pdo_statement->bindparam(':name',          $name);
        $pdo_statement->bindparam(':roll_number',   $roll_number);
        $pdo_statement->bindparam(':father_name',   $father_name);
        $pdo_statement->bindparam(':dob',           $dob);
        $pdo_statement->bindparam(':gender',        $gender);
        $pdo_statement->bindparam(':address',       $address);
        $pdo_statement->bindparam(':email',         $email);
        $pdo_statement->bindparam(':phone',         $phone);
        $pdo_statement->bindparam(':department',    $department);
        $pdo_statement->bindparam(':program',       $program);
        $pdo_statement->bindparam(':semester',      $semester);
        $pdo_statement->bindparam(':section',       $section);

        $result = $pdo_statement->execute();
    }
}
else if($count > 0) // IF TABLE EXIST -> INSERT DATA
{
    $SQL = "INSERT INTO $TABLE_NAME (name, roll_number, father_name, dob, gender, address, email, phone, department, program, semester, section) VALUES (:name, :roll_number, :father_name, :dob, :gender, :address, :email, :phone, :department, :program, :semester, :section)";
    $pdo_statement = $connect->prepare($SQL);

    $pdo_statement->bindparam(':name',          $name);
    $pdo_statement->bindparam(':roll_number',   $roll_number);
    $pdo_statement->bindparam(':father_name',   $father_name);
    $pdo_statement->bindparam(':dob',           $dob);
    $pdo_statement->bindparam(':gender',        $gender);
    $pdo_statement->bindparam(':address',       $address);
    $pdo_statement->bindparam(':email',         $email);
    $pdo_statement->bindparam(':phone',         $phone);
    $pdo_statement->bindparam(':department',    $department);
    $pdo_statement->bindparam(':program',       $program);
    $pdo_statement->bindparam(':semester',      $semester);
    $pdo_statement->bindparam(':section',       $section);

    $result = $pdo_statement->execute();

} // ELSE IF ENDS

答案 1 :(得分:0)

这与dvidxxx答案完全相同,但使用java-8个流。

List<Section> sections = outputData.getSections().stream()
             .map(Section::new)
             .collect(Collectors.toList());