JavaSE Collection
来源:互联网 发布:js实现的简单的小特效 编辑:程序博客网 时间:2024/06/04 18:06
Collection接口:
import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Date;import java.util.Iterator;import org.junit.Test;/* * 1.存储对象可以考虑:①数组 ②集合 * 2.数组存储对象的特点:Student[] stu = new Student[20]; stu[0] = new Student();.... * >弊端:①一旦创建,其长度不可变。②真实的数组存放的对象的个数是不可知。 * 3.集合 * Collection接口 * |------List接口:存储有序的,可以重复的元素 * |------ArrayList(主要的实现类)、LinkedList(对于频繁的插入、删除操作)、Vector(古老的实现类、线程安全的) * |------Set接口:存储无序的,不可重复的元素 * |------HashSet、LinkedHashSet、TreeSet * Map接口:存储“键-值”对的数据 * |-----HashMap、LinkedHashMap、TreeMap、Hashtable(子类:Properties) */public class TestCollection {@Testpublic void testCollection3() {Collection coll = new ArrayList();coll.add(123);coll.add(new String("AA"));coll.add(new Date());coll.add("BB");coll.add(new Person("MM", 23));Collection coll1 = new ArrayList();coll1.add(123);coll1.add(new String("AA"));// 10.removeAll(Collection coll):从当前集合中删除包含在coll中的元素。coll.removeAll(coll1);System.out.println(coll);// 11.equals(Object obj):判断集合中的所有元素是否完全相同Collection coll2 = new ArrayList();coll2.add(123);coll2.add(new String("AA1"));System.out.println(coll1.equals(coll2));// 12.hashCode():System.out.println(coll.hashCode());System.out.println();// 13.toArray() :将集合转化为数组Object[] obj = coll.toArray();for (int i = 0; i < obj.length; i++) {System.out.println(obj[i]);}System.out.println();// 14.iterator():返回一个Iterator接口实现类的对象,进而实现集合的遍历!Iterator iterator = coll.iterator();// 方式一:不用/* * System.out.println(iterator.next()); * System.out.println(iterator.next()); * System.out.println(iterator.next()); */// 方式二:不用// for(int i = 0;i < coll.size();i++){// System.out.println(iterator.next());// }// 方式三:使用while (iterator.hasNext()) {System.out.println(iterator.next());}}@Testpublic void testCollection2() {Collection coll = new ArrayList();coll.add(123);coll.add(new String("AA"));coll.add(new Date());coll.add("BB");// Person p = new Person("MM",23);coll.add(new Person("MM", 23));System.out.println(coll);// 6.contains(Object obj):判断集合中是否包含指定的obj元素。如果包含,返回true,反之返回false// 判断的依据:根据元素所在的类的equals()方法进行判断// 明确:如果存入集合中的元素是自定义类的对象。要求:自定义类要重写equals()方法!boolean b1 = coll.contains(123);b1 = coll.contains(new String("AA"));System.out.println(b1);boolean b2 = coll.contains(new Person("MM", 23));System.out.println(b2);// 7.containsAll(Collection coll):判断当前集合中是否包含coll中所有的元素Collection coll1 = new ArrayList();coll1.add(123);coll1.add(new String("AA"));boolean b3 = coll.containsAll(coll1);System.out.println("#" + b3);coll1.add(456);// 8.retainAll(Collection coll):求当前集合与coll的共有的元素,返回给当前集合coll.retainAll(coll1);System.out.println(coll);// 9.remove(Object obj):删除集合中的obj元素。若删除成功,返回true。否则,返回falseboolean b4 = coll.remove("BB");System.out.println(b4);}@Testpublic void testCollection1() {Collection coll = new ArrayList();// 1.size():返回集合中元素的个数System.out.println(coll.size());// 2.add(Object obj):向集合中添加一个元素coll.add(123);coll.add("AA");coll.add(new Date());coll.add("BB");System.out.println(coll.size());// 3.addAll(Collection coll):将形参coll中包含的所有元素添加到当前集合中Collection coll1 = Arrays.asList(1, 2, 3);coll.addAll(coll1);System.out.println(coll.size());// 查看集合元素System.out.println(coll);// 4.isEmpty():判断集合是否为空System.out.println(coll.isEmpty());// 5.clear():清空集合元素coll.clear();System.out.println(coll.isEmpty());}}
List接口:
import java.util.ArrayList;import java.util.List;import org.junit.Test;public class TestList {//ArrayList:List的主要实现类/* * List中相对于Collection,新增加的方法 * void add(int index, Object ele):在指定的索引位置index添加元素eleboolean addAll(int index, Collection eles)Object get(int index):获取指定索引的元素Object remove(int index):删除指定索引位置的元素Object set(int index, Object ele):设置指定索引位置的元素为eleint indexOf(Object obj):返回obj在集合中首次出现的位置。没有的话,返回-1int lastIndexOf(Object obj):返回obj在集合中最后一次出现的位置.没有的话,返回-1List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex结束的左闭右开一个子listList常用的方法:增(add(Object obj)) 删(remove) 改(set(int index,Object obj))查(get(int index)) 插(add(int index, Object ele)) 长度(size()) */@Testpublic void testList2(){List list = new ArrayList();list.add(123);list.add(456);list.add(new String("AA"));list.add(new String("GG"));list.add(456);System.out.println(list.indexOf(456));System.out.println(list.lastIndexOf(456));System.out.println(list.indexOf(123) == list.lastIndexOf(123));System.out.println(list.indexOf(444));List list1 = list.subList(0, 3);System.out.println(list1);}//List对象的CRUD@Testpublic void testList1(){List list = new ArrayList();list.add(123);list.add(456);list.add(new String("AA"));list.add(new String("GG"));System.out.println(list);//在指定的索引位置index添加元素elelist.add(0,555);System.out.println(list);//删除list.remove(0);System.out.println(list);//修改list.set(0, 111);System.out.println(list);//获取指定索引的元素Object obj = list.get(1);System.out.println(obj);}}
0 0
- JavaSE Collection
- JavaSE--Collection 源码分析
- JavaSE--08--collection--List
- 【JavaSE】day03_Date、SimpleDateFormat、Calendar、Collection
- 【javase复习】## day7 集合Collection ##
- JavaSE总结 -集合框架collection
- Javase—集合框架Collection
- javaSE-常用类-集合类Collection
- 【javaSE】Collection框架及List集合
- 黑马程序员 JavaSE-08 Collection(集合)
- 黑马程序员—javaSE—Collection集合
- JAVASE之集合(二)【Collection】
- [JavaSE-08] 集合01 - Collection接口
- JavaSE集合Collection知识点2-----ArrayList------功能
- JavaSE基础知识总结--Collection、Map集合
- 黑马程序员-JAVASE入门(集合Collection和List)
- JavaSE--集合Collection的结构体系知识点1
- <安彦>JavaSE中Collection及Map集合
- OSGI ——平台启动
- 矩阵及其运算
- linux权限篇【二】
- iOS 获取当前点击的cell相对于self.view的位置
- svm中的数学和算法
- JavaSE Collection
- Android Bluetooth蓝牙开发(二)
- Android 异步加载解决方案
- android 添加ios点击edittext之外关闭键盘功能
- linux工具篇
- 基本语法i++ ++i
- 避免Android开发中的ANR
- linux后台运行和开机启动篇【一】
- 买二手车注意事项