Java集合 -- ArrayList常用方法

public class Test_Collection {
@Test
public void Test1(){
Collection coll = new ArrayList();

// 1. size(): 返回集合中对象元素的个数
System.out.println(coll.size());//0

// 2. add(Object obj): 向集合中添加一个元素
coll.add(123);//int -> Integer 自动装箱
coll.add("AA");
coll.add(new Date());
coll.add(null);
coll.add("BB");
System.out.println(coll.size());//5

// 3. addAll(Collection coll): 将形参coll中包含的所有元素添加到当前集合中
Collection coll1 = Arrays.asList(1,2,3);
coll.addAll(coll1);
System.out.println(coll.size());//8
// 查看集合元素
System.out.println(coll);
// 4. isEmpty(): 判断集合是否为空
System.out.println(coll.isEmpty());//false

// 5. clear():清空集合元素
coll.clear();
System.out.println(coll.isEmpty());//true


}

@Test
public void Test2(){
System.out.println();

Collection coll1 = new ArrayList();
System.out.println(coll1.size());//0
coll1.add(123);//int -> Integer 自动装箱
coll1.add(new String("AA"));
coll1.add(new Date());
coll1.add("BB");
// Person p = new Person("MM",16);
// coll.add(p);
coll1.add(new Person("MM",16));
System.out.println(coll1);

// 6.contains(Object obj): 判断集合中是否包含指定的obj元素,如果包含返回true,不包含返回false
//判断的依据:根据元素所在的类的equals()方法进行判断
//明确:如果存入集合中的元素是自定义类的对象,要求自定义类重写equals()方法
boolean b = coll1.contains(123);
System.out.println(b);
b = coll1.contains("AA");
System.out.println(b);
boolean b1 = coll1.contains(new Person("MM",16));
System.out.println(b1);

// 7.containsAll(Collection coll): 判断当前集合中是否包含coll中所有的元素
Collection coll2 = new ArrayList();
coll2.add(123);
coll2.add(new String("AA"));
coll2.add(456);
boolean b2 = coll1.contains(coll2);
System.out.println(b2);

// 8.retainAll(Collection coll): 求当前集合与coll的共有的元素,返回给当前集合
coll1.retainAll(coll2);
System.out.println(coll1);

// 9.remove(Object obj)
boolean b3 = coll1.remove("BB");
System.out.println(b3);

}


public void Test3(){
Collection coll1 = new ArrayList();
// System.out.println(coll1.size());
coll1.add(123);
coll1.add(new String("AA"));
coll1.add(new Date());
coll1.add("BB");
coll1.add(new Person("MM",16));

Collection coll2 = new ArrayList();
coll2.add(123);
coll2.add(new String("AA"));

// 10.removeAll(Collection coll): 从当前集合中删除包含在coll中的元素
coll1.removeAll(coll2);
System.out.println(coll1);

// 11.equals(Object obj): 判断集合中的所有元素是否完全相同
Collection coll3 = new ArrayList();
coll3.add(123);
coll3.add(new String("AA"));
System.out.println(coll2.equals(coll2));

// 12.hashCode(): 
System.out.println(coll1.hashCode());
System.out.println();

// 13.toArray(): 将集合转化为数组
Object[] obj = coll1.toArray();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
System.out.println();

// 14.iterator(): 返回一个Iterator接口实现类的对象(迭代器),可以实现集合的遍历
Iterator iterator = coll1.iterator();

// 方式一(不用)
System.out.println(iterator.next());
System.out.println();

// 方式二(不用)
for(int i = 0; i < coll1.size(); i++){
System.out.println(iterator.next());
}
System.out.println();

// 方式三(使用)
while(iterator.hasNext()){
System.out.println(iterator.next());
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值