仅更改对象数组中的特定元素



我试图创建一个方法,改变/更新对象数组的特定元素,告诉用户插入'null'为字符串和'0'为int,这意味着默认值,而不是改变我的数组中已经存在的值。我尝试了这段代码,但是我的老师说我不应该使用standardputread来读取用户的输入。我该怎么办?

private void updateTask(String tID, String title, Date newFrom, Date newTo, status stat) 
{

StandardInputRead sir = new StandardInputRead();
int i;
displayTasks();
i = sir.readPositiveInt("Give task's index: n");
if(projectTasks[i]!=null) 
{
System.out.println("Submit only the fields you want to change.");
System.out.println("In any other case, type 'null' or '0' .");
tID = sir.readString("Change the task's ID. n");
projectTasks[i].setTaskID(tID);
title = sir.readString("Change the task's title. n");
projectTasks[i].setTaskTitle(title);
newFrom = sir.readDate("Edit the starting date (DD/MM/YY). n");
projectTasks[i].setTaskFromDate(newFrom);
newTo = sir.readDate("Edit the submission date (DD/MM/YY). n");
projectTasks[i].setTaskEndDate(newTo);
//stat = Globals.status;
projectTasks[i] = new Task(tID, title, newFrom, newTo, stat);
}
else 
System.out.println("There is no such task in our list.");

}

我也试过了,我认为这更适合我的目的:

private void updateTask(String tID, String title, Date newFrom, Date newTo, status stat) {

for(int i=0; i<this.numOfTasks; i++) {
if(projectTasks[i]!=null) {
if(tID!=null) {
projectTasks[i].setTaskID(tID);
} else if(tID ==null) {
projectTasks[i].getTaskID();
}
if(title!=null) {
projectTasks[i].setTaskTitle(title);
} else if(title==null) {
projectTasks[i].getTaskTitle();
}
if(newFrom!=null) {
projectTasks[i].setTaskFromDate(newTo);
} else if(newFrom==null) {
projectTasks[i].getTaskFromDate();
}
if(newTo!=null) {
projectTasks[i].setTaskEndDate(newTo);
} else if(newTo==null) {
projectTasks[i].getTaskEndDate();
}
if(stat!=null) {
projectTasks[i].setTaskStatus(stat);
} else if(stat==null) {
projectTasks[i].getTaskStatus();
}

projectTasks[i] = new Task(tID, title, newFrom, newTo, stat);
}
}
}

最新更新