In the us, and complete the following operations:
1) traversal collection, delete student number is 2 classmates
2) does not traverse a collection, delete student number for 3 students (delete) according to the element value
3) does not traverse a collection, judge whether the collection BaoHaoXue number for 3 students
CodePudding user response:
Solution: pay attention to by the list collection itself to remove elements, prevent individual elements were arrested, here there is pit (reason: set the list to remove elements, after the back element coordinates will move forward in turn one, the loop variable will still be + 1, one element of the fall off after removing elements), can through the way of iterator removed, forEach way, or with the method of reverse order
Import the Java. Util. ArrayList;
Import the Java. Util. List;
import java.util.Scanner;
Import the Java. Util. The function. The Predicate;
/* *
* @ the ClassName: Student
* @ Description: TODO
* @ author: MZB
* @ date: March 19, 2020 morning 9:26:51
*/
Public class Student {
Private Integer stuNo;
Private Integer stuScore;
Public Integer getStuNo () {
Return stuNo;
}
Public void setStuNo (Integer stuNo) {
Enclosing stuNo=stuNo;
}
Public Integer getStuScore () {
Return stuScore;
}
Public void setStuScore (Integer stuScore) {
Enclosing stuScore=stuScore;
}
@ Override
Public int hashCode () {
Final int prime=31;
Int result=1;
Result=prime * result + ((stuNo==null)? Zero: stuNo hashCode ());
Result=prime * result + ((stuScore==null)? Zero: stuScore hashCode ());
Return the result.
}
@ Override
Public Boolean equals (Object obj) {
If (this==obj)
return true;
If (obj==null)
return false;
If (getClass ()!=obj. GetClass ())
return false;
Student other=(Student) obj;
If (stuNo==null) {
If (other stuNo!=null)
return false;
} else if (! StuNo. Equals (other. StuNo))
return false;
If (stuScore==null) {
If (other stuScore!=null)
return false;
} else if (! StuScore. Equals (other. StuScore))
return false;
return true;
}
@ Override
Public String toString () {
Return "Student [stuNo=" + stuNo + ", stuScore="+ stuScore +"] ".
}
Public static void main (String [] args) {
Scanner scan=new Scanner(System.in);
ListList=new ArrayList<> (a);
//collection initialise
for(int i=0; i<3; I++) {
System. The out. Print (" both please input stuNo: ");
Int stuNo=scan. NextInt ();
System. The out. Print (" both please input stuScore: ");
Int stuScore=scan. NextInt ();
Student stu=new Student ();
Stu. SetStuNo (stuNo);
Stu. SetStuScore (stuScore);
List. The add (stu);
}
System.out.println();
System. The out. Println (" 0: "+ list);
//1 through collection, delete student number is 2 classmates
For (int I=list. The size () - 1; I>=0; I -) {
Student stu=list. Get (I);
If (2==stu. GetStuNo ()) {
List. Remove (I);
}
}
System. The out. Println (" 1: "+ list);
//2 does not traverse a collection, delete student number for 3 students
List. RemoveIf (new Predicate() {
@ Override
Public Boolean test (Student stu) {
//TODO Auto - generated method stub
Return stu. GetStuNo ()==3;
}
});
System. The out. Println (" 2: "+ list);
//3 does not traverse a collection, whether the collection contains student id for one of his classmates
A Boolean flag=false;
For (Student stu3: list) {
If (1==stu3 getStuNo ()) {
Flag=true;
}
}
System. The out. Println (" 3: "+" : "+ + list flag);
}
}
The third question: not by iterating through collection, whether the collection contains student number for the 3 students,, not very convenient,
CodePudding user response: