Мне нужно сделать функцию, чтобы добавить новых учеников в arraylist и функцию, чтобы удалить студентов из arraylist, используя их идентификатор студента. Любая помощь будет принята с благодарностью! Эта проблема уже решена.Написание функции для добавления записи и функции для удаления записи из arraylist в Java
public class Main {
public static void main(String[] args) throws Exception {
String stdID;
int tScore1;
int tScore2;
int tScore3;
String sName;
Students workobj;
try{
//opening the file for input
FileInputStream istream = new FileInputStream("input.txt");
Scanner input = new Scanner(istream);
//creating an arraylist to store student objects
ArrayList<Students> AllStudents= new ArrayList<Students>();
while(input.hasNextLine()){
stdID=input.next();
sName=input.next();
tScore1=input.nextInt();
tScore2=input.nextInt();
tScore3=input.nextInt();
Students StudentRecord= new Students(stdID,sName,tScore1,tScore2,tScore3);
StudentRecord.listStudents();
AllStudents.add(StudentRecord);
}
for(int i=0;i<=AllStudents.size()-1;i++){
workobj=AllStudents.get(i);
workobj.listStudents();
}
deleteStudent("99I9");
sortLarge(AllStudents);
for(int i=0; i<=AllStudents.size()-1;i++){
workobj=AllStudents.get(i);
workobj.listStudents();
}
}
catch (FileNotFoundException e){
System.out.println("file not found");
System.err.println("File not found");
System.exit(11);
}
catch (InputMismatchException e){
System.out.println("Error in Reading File");
System.err.println("Error in Reading File");
System.exit(10);
}
finally {
output.close();
System.exit(2);
}
}
public static class Students{
String studentID;
String Name;
int TestScore01;
int TestScore02;
int TestScore03;
double tAvg;
String tGrade;
public Students(String stdID, String sName, int tScore1,int tScore2, int tScore3){
studentID=stdID;
Name=sName;
TestScore01=tScore1;
TestScore02=tScore2;
TestScore03=tScore3;
tAvg = ((tScore1+ tScore2 + tScore3)/300.0)*100;
}
public void listStudents(){
//this function lists all of the elements of the student
System.out.println("\nNew Student\n Student ID: "+studentID);
System.out.println(" Student Name: "+ Name);
System.out.println(" Test 1 Score: " + TestScore01);
System.out.println(" Test 2 Score: " +TestScore02);
System.out.println(" Test 2 Score:" + TestScore03);
System.out.println(" Average Test Score " +tAvg);
System.out.println(" Grade: " + tGrade);
}//end of listStudents()
public int compareTo(Object o){
//this functions compares the average test score of two students
if (gettAvg()<((Students)o).gettAvg()) return 1;
else
if(gettAvg()>((Students)o).gettAvg())return-1;
else return 0;
}
}
}
public static void sortLarge(ArrayList<Students>mylist){
Students xsave, ysave;
int isw=1, xlast=mylist.size();
while (isw==1){isw=0;
for (int i=0;i<=xlast-2;i++){
if (((mylist.get(i)).compareTo((mylist.get(i+1)))==1)){
xsave=mylist.get(i);
ysave=mylist.get(i+1);
mylist.remove(i);
mylist.add(i,ysave);
mylist.remove(i+1);
mylist.add(i+1,xsave);
isw=1;
}
}
}
return;
}
}
и в чем проблема? что ты сделал до сих пор? – chenchuk
public static void addStudent (ArrayList AllStudents, Students workobj) { возвращение; } public static void deleteStudent (ArrayList studentsArrayList, String stdID) { studentsArrayList.remove (stdID); } Я пробовал несколько вещей, и я не уверен, что я делаю неправильно. Я не уверен, что deleteStudent (ArrayList studentsArrayList, String stdID) - это правильная вещь. Я также не уверен в addStudent. –
benderbot2004