0

This is the code I found online and was wondering How am I suppose to find exact value, for example I am using scanner, so if the user in this case enters 2, then I want to remove that row whose roll number is 2. So then it would only print 1 AAA 13, and 3 CCC 15.

 public static void main(String[] args) {

            //Creating user defined class objects  
            Student s1=new Student(1,"AAA",13);  
            Student s2=new Student(2,"BBB",14);  
            Student s3=new Student(3,"CCC",15); 

            ArrayList<Student> al=new ArrayList<Student>();
            al.add(s1);
            al.add(s2);  
            al.add(s3);  

            Iterator itr=al.iterator();  

            //traverse elements of ArrayList object  
            while(itr.hasNext()){  
                Student st=(Student)itr.next();  
                System.out.println(st.rollno+" "+st.name+" "+st.age);  
            }  
        }
    }

    class Student{  
        int rollno;  
        String name;  
        int age;  
        Student(int rollno,String name,int age){  
            this.rollno=rollno;  
            this.name=name;  
            this.age=age;  
        }  
    } 
PSKP
  • 1,178
  • 14
  • 28
  • 1
    Does this answer your question? [Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – Nowhere Man Jun 18 '20 at 03:51
  • 1
    You should be good using `if (st.rollno == 2) itr.remove();` – Nowhere Man Jun 18 '20 at 03:54

0 Answers0