设置数组的元素,将其他元素设置为同一数组

  • 本文关键字:元素 数组 设置 其他 android
  • 更新时间 :
  • 英文 :


我有一节课" college ";有一个内部类" prof "数组参数;这个内部类有一个字符串"Materials"数组参数,我使用" addprof "为了将教授添加到外部类数组中,现在在这个add教授方法中,我创建了一个新教授,并将其材料设置为传递的数组,但是每次我添加一个新教授时,数组中的所有教授都具有相同的材料数组!

现在是教授班

private class Professor{
private String Name;
private ArrayList<String> Materials;
private String Department;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public ArrayList<String> getMaterial() {
return Materials;
}
public void setMaterial(ArrayList<String> material) {
Materials = material;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Professor{" + "Name='").append(Name).append(''').append("Material: ");
for(String mat : Materials){
Log.d("Material",mat);
builder.append(mat).append(",");
}
builder.append("Department: ").append(Department);
return builder.toString();
}
}

这里是add方法

public void addProfessor(String Name,@Nullable String department,@Nullable ArrayList<String> materials){
Professor professor = new Professor();
professor.setDepartment(department);
professor.setMaterial(materials);
professor.setName(Name);
professorsMap.put(Name,professor);
Professors.add(professor);
for(String mat :materials){
Log.d("ADDPROF",mat);
}
for(Professor prof : Professors){
Log.d("MatProf",prof.toString());
}
}

我添加的代码(我暗示了与教授数组无关的代码)

ArrayList<String> profMaterials = new ArrayList<>();
submitMat.setOnClickListener(v -> {
if(!professorMaterialLayout.getEditText().getText().toString().isEmpty()) {
profMaterials.add(professorMaterialLayout.getEditText().getText().toString());
professorMaterialLayout.getEditText().setText("");
}
});
submitTeacher.setOnClickListener(v -> {
String counterString = staffNumber.getText().toString();
int Count = Integer.parseInt(counterString.subSequence(counterString.indexOf("#")+1,counterString.length()).toString());
if(!professorNum.getText().toString().isEmpty()&&!assistantNumb.getText().toString().isEmpty()) {
if (Count == Integer.parseInt(professorNum.getText().toString())&&counterString.contains("Professor")) {
college.addProfessor(professorNameLayout.getEditText().getText().toString()
,professorDepartmentLayout.getEditText().getText().toString()
,profMaterials);

profMaterials.clear();
}else{

college.addProfessor(professorNameLayout.getEditText().getText().toString()
,professorDepartmentLayout.getEditText().getText().toString()
,profMaterials);
profMaterials.clear();

}
}
});*emphasized text*

您没有在任何地方创建Material数组列表的新实例,也在Professor中,您直接引用传递的Material列表,这对于所有Professors都是相同的,将您的addProfessor方法修改为以下

public void addProfessor(String Name,@Nullable String department,@Nullable ArrayList<String> materials){
Professor professor = new Professor();
professor.setDepartment(department);
//create new instance for each professor
professor.setMaterial(new ArrayList<>(materials));
professor.setName(Name);
professorsMap.put(Name,professor);
Professors.add(professor);
for(String mat :materials){
Log.d("ADDPROF",mat);
}
for(Professor prof : Professors){
Log.d("MatProf",prof.toString());
}
}

相关内容

  • 没有找到相关文章

最新更新