英文:
How can I print the array list objects befor and after deletion
问题
这是我的删除以字母 O 开头的对象的方法:
public static void DeleteO(ArrayList<Students> students) {
for (Iterator<Students> iterator = students.iterator(); iterator.hasNext(); ) {
Students student = iterator.next();
if (student.getName().startsWith("O")) {
iterator.remove();
}
}
}
这是我的打印方法:
public static void PrintContent(ArrayList<Students> students) {
int x = 1;
for (Students student : students){
System.out.println("学生编号为 " + (x++) + ",姓名为:" + student.name + ",分数为:" + student.mark + "。");
}
}
如何在删除前后打印我的 ArrayList 元素?我在进行打印时执行以下操作:
Students.PrintContent(students);
英文:
This is my method for deleting objects starts with O:
public static void DeleteO(ArrayList<Students> students) {
for (Iterator<Students> iterator = students.iterator(); iterator.hasNext(); ) {
Students student = iterator.next();
if (student.getName().startsWith("O")) {
iterator.remove();
}
And here is my Printing method:
public static void PrintContent(ArrayList<Students> students) {
int x = 1;
for (Students student : students){
System.out.println("The student number " + (x++) +" is: "+student.name + " and his/her mark is: " + student.mark + ".");
}}
How can I print my arraylist elemnts before and after the deletion? For printing I am doing the folowing:
Students.PrintContent(students);
答案1
得分: 1
package test;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
ArrayList<Students> al = new ArrayList<>();
al.add(new Students("Oussema", 10));
al.add(new Students("Ahmed", 15));
al.add(new Students("Omayma", 13));
al.add(new Students("ali", 17));
System.out.println("________Print Content before delete________");
Students.PrintContent(al);
Students.DeleteO(al);
System.out.println("________Print Content after delete________");
Students.PrintContent(al);
}
}
class Students {
String name;
double mark;
public Students(String name,double mark) {
this.name = name;
this.mark = mark;
}
public static void DeleteO(ArrayList<Students> students) {
for (Iterator<Students> iterator = students.iterator(); iterator.hasNext();) {
Students student = iterator.next();
if (student.name.startsWith("O")) {
iterator.remove();
}
}
}
public static void PrintContent(ArrayList<Students> students) {
int x = 1;
for (Students student : students) {
System.out.println("The student number " + (x++) + " is: " + student.name + " and his/her mark is: " + student.mark + ".");
}
}
}
英文:
package test;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
ArrayList<Students> al = new ArrayList<>();
al.add(new Students("Oussema", 10));
al.add(new Students("Ahmed", 15));
al.add(new Students("Omayma", 13));
al.add(new Students("ali", 17));
System.out.println("________Print Content before delete________");
Students.PrintContent(al);
Students.DeleteO(al);
System.out.println("________Print Content after delete________");
Students.PrintContent(al);
}
}
class Students {
String name;
double mark;
public Students(String name,double mark) {
this.name = name;
this.mark = mark;
}
public static void DeleteO(ArrayList<Students> students) {
for (Iterator<Students> iterator = students.iterator(); iterator.hasNext();) {
Students student = iterator.next();
if (student.name.startsWith("O")) {
iterator.remove();
}
}
}
public static void PrintContent(ArrayList<Students> students) {
int x = 1;
for (Students student : students) {
System.out.println("The student number " + (x++) + " is: " + student.name + " and his/her mark is: " + student.mark + ".");
}
}
}
Output :
________Print Content before delete________
The student number 1 is: Oussema and his/her mark is: 10.0.
The student number 2 is: Ahmed and his/her mark is: 15.0.
The student number 3 is: Omayma and his/her mark is: 13.0.
The student number 4 is: ali and his/her mark is: 17.0.
________Print Content after delete________
The student number 1 is: Ahmed and his/her mark is: 15.0.
The student number 2 is: ali and his/her mark is: 17.0.
or in delete methode you can call PrintContent methode like this :
public static void DeleteO(ArrayList<Students> students) {
Students.PrintContent(students);
for (Iterator<Students> iterator = students.iterator(); iterator.hasNext();) {
Students student = iterator.next();
if (student.name.startsWith("O")) {
iterator.remove();
}
}
Students.PrintContent(students);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论