写 SSD3 遇见很 囧 的事情

事件Ⅰ:
   从类的数组中找出某个元素。vivizhyy 搞了件很 囧 的事情,导致代码写的很 cuo……
这个其实可以用 map 做,设置 key. 但是懒得去搞那个东东,就用循环吧。循环完了之后,vivizhyy 忘记设置空的返回值,导致编译老出错,自己还郁闷半天~囧
那个函数应该是这样写的:
  public static Employee getEmployee(Employee[] array, int id) {
        int len = array.length;
        if (len == 0) {
            return null;
        }
        int i = 0;
        for (; i < len; i++) {
            if (array[i].getId() == id) {
                return array[i];
            }
        }
        return null;
}

事件Ⅱ:
我竟然在 iterator.remove() 里面传了参数!-_-|||然后竟然在这个很猪的问题上卡了一天……
以后遇见问题的时候先查 api……

Iterators

The ArrayList method iterator returns a java.util.Iterator<E> object over the elements of the collection. An iterator is an object for traversing a collection from start to finish. In addition, using iterators you can safely removing elements from the collection during the traversal.

The class java.util.Iterator<E> provides the following methods:

  • E next(). Returns the next element in the iteration.
  • void remove(). Removes from the collection the last element returned by the iterator. This method throws the exception IllegalStateException, if the method next has not been called, or the method remove has already been called after the last call to the method next.

For example, the following code concatenates the strings in a collection and then displays the result: "ArrayList and Iterators".


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值