How to remove specific object from ArrayList in Java?

前端 未结 13 1639
旧时难觅i
旧时难觅i 2020-12-05 06:54

How can I remove specific object from ArrayList? Suppose I have a class as below:

import java.util.ArrayList;    
public class ArrayTest {
    int i;

    pu         


        
13条回答
  •  温柔的废话
    2020-12-05 07:56

    AValchev is right. A quicker solution would be to parse all elements and compare by an unique property.

    String property = "property to delete";
    
    for(int j = 0; j < i.size(); j++)
    {
        Student obj = i.get(j);
    
        if(obj.getProperty().equals(property)){
           //found, delete.
            i.remove(j);
            break;
        }
    
    }
    

    THis is a quick solution. You'd better implement object comparison for larger projects.

提交回复
热议问题