如何在删除之前和之后打印数组列表中的对象。

huangapple go评论86阅读模式
英文:

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&lt;Students&gt; students) {
 for (Iterator&lt;Students&gt; iterator = students.iterator(); iterator.hasNext(); ) {
Students student = iterator.next();
            if (student.getName().startsWith(&quot;O&quot;)) {
    iterator.remove();
    
}

And here is my Printing method:

public static void PrintContent(ArrayList&lt;Students&gt; students) {
    int x = 1;
    for (Students student : students){
             System.out.println(&quot;The student number &quot; + (x++) +&quot; is: &quot;+student.name + &quot; and his/her mark is: &quot; + student.mark + &quot;.&quot;); 

    }}

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&lt;Students&gt; al = new ArrayList&lt;&gt;();
al.add(new Students(&quot;Oussema&quot;, 10));
al.add(new Students(&quot;Ahmed&quot;, 15));
al.add(new Students(&quot;Omayma&quot;, 13));
al.add(new Students(&quot;ali&quot;, 17));
System.out.println(&quot;________Print Content before delete________&quot;);
Students.PrintContent(al);
Students.DeleteO(al);
System.out.println(&quot;________Print Content after delete________&quot;);
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&lt;Students&gt; students) {
for (Iterator&lt;Students&gt; iterator = students.iterator(); iterator.hasNext();) {
Students student = iterator.next();
if (student.name.startsWith(&quot;O&quot;)) {
iterator.remove();
}
}
}
public static void PrintContent(ArrayList&lt;Students&gt; students) {
int x = 1;
for (Students student : students) {
System.out.println(&quot;The student number &quot; + (x++) + &quot; is: &quot; + student.name + &quot; and his/her mark is: &quot; + student.mark + &quot;.&quot;);
}
}
}

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&lt;Students&gt; students) {
Students.PrintContent(students);
for (Iterator&lt;Students&gt; iterator = students.iterator(); iterator.hasNext();) {
Students student = iterator.next();
if (student.name.startsWith(&quot;O&quot;)) {
iterator.remove();
}
}
Students.PrintContent(students);
}

huangapple
  • 本文由 发表于 2020年10月14日 08:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64344952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定