Holding you objects

1.Hold Objects

用于装载对象的可以分为Array和Container。

Array元素不仅可以为对象,还可以为基本类型。Container元素不可以为基本类型。

Container分类为Collection和Map,其中Collection包括List,Set,Queue。

Collection : a sequence of individual elements with one or more rules applied to them. AListmust hold the elements in the way that they were inserted, aSet cannot have duplicate elements, and a Queue produces the elements in the order determined by a queuing discipline (usually the same order in which they are inserted).

Map : a group of key-value object pairs, allowing you to look up a value using a key. 

2.Collection Add Elements

向Collection添加元素时,可以调用成员函数Collection.addAll(),或者java.util包下的Collections.addAll()

Collection.addAll()要求参数为Collection。

Collections.addAll()更加灵活,参数可以为Collection,Array,或comma-seperated的元素

Arrays.asList用户将Array转化为Collection(List),可以作为使用Collection API的桥梁。但注意两点

     (1).返回的List为Fix-Sized,任何add和remove操作都将导致Unsupported Operation Exception

     (2).根据成员的类型,Best-Guess 返回List的类型。这可能与预期的类型要求不同

3.List

对List接口的常见实现包括ArrayList和LinkedList。

在List中Add、Remove、indexOf等操作时,通过调用对象的equals()方法实现的。

4.Iterator

实现iterable接口,需要实现Iterator<E> iterator()方法。Iterator类需要实现hasNext(), next()方法。这个也是能够使用foreach语法的前提。

List接口中,还实现了ListIterator<E> listIterator()方法。可以双向遍历。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值