वस्तुओं को हटाने के लिए यह मेरा तरीका है 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("The student number " + (x++) +" is: "+student.name + " and his/her mark is: " + student.mark + "."); 

    }}

हटाने से पहले और बाद में मैं अपनी सरणीसूची के तत्वों को कैसे प्रिंट कर सकता हूं? छपाई के लिए मैं निम्नलिखित कार्य कर रहा हूँ:

Students.PrintContent(students);
-2
Allo0o2a 14 अक्टूबर 2020, 03:30

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 + ".");

        }
    }
}

आउटपुट:

________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.

या डिलीट मेथोड में आप PrintContent मेथोड को इस तरह कॉल कर सकते हैं:

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);
}
1
aziz k'h 14 अक्टूबर 2020, 03:59