JavaSE之简单集合的使用

/**
* Collection体系集合
* 1)List集合:有下标,元素可重复
* ArrayList: 数据结构实现,查询快,增删慢(运行效率快,线程不安全)
* Vector: 数据结构实现,查询快,增删慢(运行效率慢,线程安全)
* LinkedList: 链表结构实现,增删快,查询慢
* 2)Set集合:无下标,元素不可重复
* HashSet: 基于hashcode实现元素不重复;当元素的哈希码相等时,会调用==或equals确认,相等则拒绝存入
* LinledHashSet: 链表实现HashSet,按照链表存储,可保留元素的插入顺序
* TreeSet: 可以排序, 但是需要指定排序的算法
*
* 泛型:泛型类、泛型接口、泛型方法
* <T>泛型:类型占位符,表示一种引用类型
* 泛型优点:提高代码重用性、防止类型转换异常,提高代码的安全性
*/

 

 1 class StudentTest {
 2     //实体类
 3     private String name;
 4     private int    age;
 5     //无参构造
 6     public StudentTest() {}
 7     //有参构造
 8     public StudentTest(String name,int age) {
 9         this.name = name;
10         this.age = age;
11     }
12     // set/get方法
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public int getAge() {
22         return age;
23     }
24 
25     public void setAge(int age) {
26         this.age = age;
27     }
28     //toString(作用):返回一个字符串,将该Java对象的内存地址经过哈希算法得出int类型的值;再将其转换成十六进制。
29     @Override
30     public String toString() {
31         return "Student [name=" + name + ", age=" + age + "]";
32     }
33 }

 

/**
* 1、ArrayList的简单使用
*/

 1 public class GatherTest {
 2     public static void main(String[] args) {
 3         //使用集合Collection保存StudentTest对象
 4         Collection<StudentTest> coll = new ArrayList<>();
 5         coll.add(new StudentTest("zs",20));
 6         coll.add(new StudentTest("ls",22));
 7         coll.add(new StudentTest("ww",16));
 8 
 9         System.out.println("==========使用迭代器进行遍历输出==========>");
10         Iterator<StudentTest> it = coll.iterator();
11         while (it.hasNext()) {
12             System.out.println(it.next());
13         }
14 
15         System.out.println("==========使用增强for循环进行遍历输出==========>");
16         for (StudentTest student : coll) {
17             System.out.println(student);
18         }
19 
20         System.out.println("---------------清除所有对象--------------");
21         coll.clear();
22         System.out.println(coll);
23 
24         System.out.println("---------------判断是否为空--------------");
25         System.out.println(coll.isEmpty());
26     }
27 }

 

/**
* 2、ArrayList的简单使用
*/

 1 class ArrayTest {
 2     public static void main(String[] args) {
 3         List<StudentTest> list = new ArrayList<>();
 4         list.add(new StudentTest("zs",20));
 5         list.add(new StudentTest("ls",22));
 6         Iterator<StudentTest> iterat = list.iterator();
 7         while (iterat.hasNext()) {
 8             System.out.println(iterat.next());
 9         }
10         System.out.println("---------添加、以下标添加----------");
11         list.add(1,new StudentTest("zw",19));
12         Iterator<StudentTest> iterat1 = list.iterator();
13         while (iterat1.hasNext()) {
14             System.out.println(iterat1.next());
15         }
16         System.out.println("---------下标获取对象并输出----------");
17         System.out.println(list.get(1));
18 
19         System.out.println("---------以下标删除----------");
20         list.remove(2);
21         Iterator<StudentTest> iterat2 = list.iterator();
22         while (iterat2.hasNext()) {
23             System.out.println(iterat2.next());
24         }
25         System.out.println("---------以下标修改值----------");
26         list.set(1, new StudentTest("ls",22));
27         Iterator<StudentTest> iterat3 = list.iterator();
28         while (iterat3.hasNext()) {
29             System.out.println(iterat3.next());
30         }
31 
32     }
33 }

 

/**
* 3、LinkedList的简单使用
*/

 1 class Gather {
 2     public static void main(String[] args) {
 3         LinkedList<StudentTest> at = new LinkedList<>();
 4         at.add(new StudentTest("zs",20));
 5         System.out.println("---------添加、以下标添加----------");
 6         at.addLast(new StudentTest("ls",22));
 7         at.add(1,new StudentTest("ww",18));
 8         Iterator<StudentTest> it = at.iterator();
 9         while(it.hasNext()) {
10             StudentTest st = it.next();
11             System.out.println(st.getName()+"--->"+st.getAge());
12         }
13         System.out.println("---------下标获取对象并输出----------");
14         System.out.println(at.get(1));
15 
16         System.out.println("---------以下标删除----------");
17         at.remove(1);
18         Iterator<StudentTest> iterat2 = at.iterator();
19         while (iterat2.hasNext()) {
20             System.out.println(iterat2.next());
21         }
22         System.out.println("---------以下标修改值----------");
23         at.set(1, new StudentTest("wz",17));
24         Iterator<StudentTest> iterat3 = at.iterator();
25         while (iterat3.hasNext()) {
26             System.out.println(iterat3.next());
27         }
28     }
29 }

 

//4、案例: 集合存储字符串元素,例如: "ldh","guodegang","lyf","gyy","liuyifei"
//找出集合中字符串长度为3的元素;
// 找出集合中字符串长度大于5的元素;

 1 class ListTest {
 2         public static void main(String[] args) {
 3             //将字符串元素用数组存储
 4             String[] as = {"ldh","guodegang","lyf","gyy","liuyifei"};
 5             //将数组转换为List集合
 6             List<String> list = Arrays.asList(as);
 7             List<String> list2 = new ArrayList<String>();
 8             List<String> list3 = new ArrayList<String>();
 9             //用循环遍历判断
10             for(String a: list) {
11                 if(a.length()==3){
12                     list2.add(a);
13                 }else if(a.length()>5) {
14                     list3.add(a);
15                 }
16             }
17             System.out.println("长度为3的元素:"+list2+"\n"+"长度大于5的元素:"+list3);
18         }
19 }

 

posted @ 2021-04-24 14:41  夜景&  阅读(85)  评论(0)    收藏  举报