前言:

最近遇到的一道很基础的题,有时候大家可能离开了编译器就不行了。

import java.util.List;

/**
 * 
 * @author catchegg
 * create date: 2018年6月1日 下午10:16:08
 */
public class TestClear {
    public TestClear() {}   
    public static void main(String[] args) {
        int value = 0;
        List<int> l = new List<int>();
        for(int i : l){
            if(i==value){
                l.remove(i);
            }
        }
    }
}

错误:

  1. 泛形要求能包容的是对象类型,而基本类型在java里不属于对象,可以使用基本类型的包装类代替。List<Integer> l = new List<>();
  2. List是接口,不能实例化对象,可以改成其实现类ArrayList。List<Integer> l = new ArrayList<>();
  3. for-each不能删除,要用iterator。

相关文章:

  • 2021-11-30
  • 2022-02-11
  • 2022-01-17
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2021-06-07
猜你喜欢
  • 2021-05-29
  • 2021-10-09
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
相关资源
相似解决方案