Java集合类是我们在工作中运用最多的、最频繁的类。相比于数组(Array)来说,集合类的长度可变,更加适合于现代开发需求; Java集合主要由2大体系构成,分别是Collection体系和Map体系,其中Collection和Map分别是2大体系中的顶层接口。 Collection主要有三个子接口,分别为List(列表)、Set(集)、Queue(队列)。其中,List、Queue中的元素有序可重复,而Set中的元素无序不可重复; List中主要有ArrayList、LinkedList两个实现类;Set中则是有HashSet实现类;而Queue是在JDK1.5后才出现的新集合,主要以数组和链表两种形式存在。 Map同属于java.util包中,是集合的一部分,但与Collection是相互独立的,没有任何关系。Map中都是以key-value的形式存在,其中key必须唯一,主要有HashMap、HashTable、TreeMap三个实现类。 创建对象:与其他普通的引用数据类型创建方式完全相同,但是要指定容器中存储的数据类型: 注意: Collection:单列集合的根接口目录
前言
数组与集合区别
集合描述
Java集合就像一个容器,可以存储任何类型的数据,也可以结合泛型来存储具体的类型对象。在程序运行时,Java集合可以动态的进行扩展,随着元素的增加而扩大。在Java中,集合类通常存在于java.util包中。集合格式
ArrayList<要存储元素的数据类型> 变量名 = new ArrayList<要存储元素的数据类型>();
集合中存储的元素,只能为<>括号中指定的数据类型元素; “<要存储元素的数据类型>”中的数据类型必须是引用数据类型,不能是基本数据类型;
方法
size() 长度 get()获取 add()添加 addALL() 将一个集合添加到另一个集合 remove()删除remove(new integer(1)) removeAll() 以集合的形式删除元数—— remove(index) clear() 清空集合中的元素 isEmpty()集合中元素是否为空 iterator 迭代器 toArray 将集合中元素转换成数组 add(index,element) add(0,10)0是索引10是新填值 indexof 返回索引值 contains(e)判断集合中是否有莫个元素 set(index,element) 修改集合中元素 sublist 获取集合中摸一范围的元素,以集合的形式返回,不包含结束索引
ArrayList实例
1.add(E e)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class AarryList_clear_isEmpty { public static void main(String[] args) { //声明集合 List<Integer>list=new ArrayList<>(); //添加元素 list.add(1); list.add(2); list.add(3); //列表中删除所有元素。 //list.clear(); //判断集合是否为空 boolean f=list.isEmpty();// System.out.println(f);//false 如果将list.clear();注释去掉结果就为true } }
2.add(int index, E element)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayLiat_add { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); //add列表中的指定位置插入指定的元素 list.add(0,10); int num=list.get(0); System.out.println("索引为0的元素:"+num);//索引为0的元素:10 for (Integer integer : list) { System.out.println(integer);//10 1 2 3 } } }
3.addAll(Collection<? extends E> c)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class Arraylist_addAll { public static void main(String[] args) { /* * 将一个集合添加到另一个集合 * */ //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); //声明集合 List<Integer>list1 =new ArrayList<>(); //将集合list添加到list1中 list1.addAll(list); //长度 int nums=list1.size(); System.out.println("长度 :"+nums);//长度 :3 System.out.println(list1);//[1, 2, 3] } }
4.clear() 与 isEmpty()
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class AarryList_clear_isEmpty { public static void main(String[] args) { //声明集合 List<Integer>list=new ArrayList<>(); //添加元素 list.add(1); list.add(2); list.add(3); //列表中删除所有元素。 //list.clear(); //判断集合是否为空 boolean f=list.isEmpty();// System.out.println(f);//false 如果将list.clear();注释去掉结果就为true } }
5.contains(Object o)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayList_contains { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); //contains(e)判断集合中是否有某个元素 boolean b=list.contains(new Integer(1)); System.out.println(b);//true boolean b1=list.contains(1); System.out.println(b1);//true } }
6.indexOf(Object o)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayList_indexof { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); list.add(3); list.add(3); //返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。 int num=list.indexOf(new Integer(1)); System.out.println(num);//0 /* * indexof 寻找只是第一个3所以索引就都是2 * */ int num1=list.indexOf(3); System.out.println(num1);//2 int num2=list.indexOf(3); System.out.println(num2);//2 int num3=list.indexOf(6); System.out.println(num3);//-1 说明该元素不存在集合中 } }
7.iterator()
package 集合.ArrayList; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class arrayList_iterator { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); list.add(3); list.add(3); // Iterator<Integer> integers =list.iterator(); /* * integers.hasNext()//用来检测迭代器中是否有元素返回true flase * for 循环是明确次数条件下 * 而当前没有明确次数 * 所有用while 只要条件成立就执行 * */ while (integers.hasNext()){ int num=integers.next();//获取迭代器中的元素 if (num==3){ integers.remove();//删除所有元素 } } //加强for循环 for (Integer integer : list) { System.out.println(integer); } } }
8.remove(int index)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayList_remove { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); list.remove(1); list.remove(new Integer(2));//指定需要删除元素,可它存在一个移位问题无法解决重复元素的删除 //返回此列表中的元素数。 int num=list.size(); //输出集合中元素个数 System.out.println("元素个数"+num);//元素个数2 System.out.println(list);//[1, 3] } }
9.removeAll(Collection<?> c)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class Arraylist_removeAll { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); list.add(4); //声明集合 List<Integer>list1 =new ArrayList<>(); list1.add(1); list1.add(2); //从此列表中删除指定集合中包含的所有元素 list1.removeAll(list1); for (Integer integer : list) { System.out.println(integer);//1 2 3 4 } //长度 int nums=list.size(); System.out.println("长度 :"+nums);//长度 :4 System.out.println(list);//[1, 2, 3, 4] } }
10.remove(int index) 移位
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class Arraylist_remove移位 { public static void main(String[] args) { List<Integer>list=new ArrayList<Integer>(); list.add(1); list.add(2); list.add(2); list.add(3); //遍历 for(int i = list.size()-1;i >=0;i--){ if (list.get(i)==2){ list.remove(i);//移除2 } } //遍历集合 for (Integer integer : list) { System.out.println(integer); } } }
11.set(int index, E element)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayList_set { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); list.set(0,19);//用指定的元素替换此列表中指定位置的元素 int num=list.get(0); System.out.println(num);//19 for (Integer integer : list) { System.out.println(integer);//19 2 3 } } }
12. subList(int fromIndex, int toIndex)
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayList_subList { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); //获取集合中摸一范围的元素,以集合的形式返回,不包含结束索引 List<Integer>num=list.subList(0,2); for (Integer integer : num) { System.out.println(integer);//1 2 } } }
13.toArray()
package 集合.ArrayList; import java.util.ArrayList; import java.util.List; public class ArrayList_toArray { public static void main(String[] args) { //声明集合 List<Integer> list =new ArrayList<>(); //向集合中添加元素 list.add(1); list.add(2); list.add(3); Object[] num =list.toArray(); //将集合中元素转换成数组 //试试length, System.out.println("数组长:"+num.length);//数组长:3 //遍历数组 for (Object o : num) { System.out.print(o);//123 } } }
14.sort(Comparator<? super E> c)
package 集合.ArrayList; import java.util.Arrays; public class Test { public static void main(String[] args) { //声明数组 int[]nums={1,4,3,5,67,7,8}; //输出 System.out.println(Arrays.toString(nums));//toString()是一个静态方法 Arrays.sort(nums); System.out.println(Arrays.toString(nums)); } }
LinkedList实例
1.常用方法
package 集合.LinkedList; import java.util.LinkedList; import java.util.List; /** * @author fengxun * @version 1.0 * @date 2020/4/10 15:35 */ public class linkedLink { public static void main(String[] args) { //声明集合 List<Integer>list=new LinkedList<Integer>(); //添加 list.add(1); list.add(2); list.add(3); //获取集合中有几个元素 int num=list.size(); System.out.println("获取集合中有几个元素"+num);//3 // int num1=list.get(1); System.out.println(num1);//索引为1的值为2 } }
package 集合.LinkedList; import java.util.LinkedList; import java.util.List; public class Linkedlist { public static void main(String[] args) { //声明集合 List<Integer> list=new LinkedList<Integer>(); //添加 list.add(1); list.add(2); list.add(3); //清空元素 //list.clear(); // 将集合种元素转换数组 //Object[] obj =list.toArray(); /*for (Object o : obj) { System.out.println(o);//123 }*/ //获取元素索引 int num=list.indexOf(new Integer(1)); System.out.println(num);//0 //判断集合中是否包含元素(返回bollean) boolean contains=list.contains(1); System.out.println(contains);//true //修改指定索引值 list.set(0,3); for (Integer integer : list) { System.out.println(integer);//3 2 3 } } }
2.remove()之移位方案
package 集合.LinkedList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class LinkedList_remove { public static void main(String[] args) { //声明集合 List<Integer> list=new LinkedList<Integer>(); //添加 list.add(1); list.add(2); list.add(2); list.add(3); //remove删除元素(弊端:当有重复元素时会漏删) /* list.remove(new Integer(2)); for (Integer integer : list) { System.out.println(integer);//输出123漏删一个二 }*/ //从后往前删 /* for(int i=list.size()-1;i>=0;i--){ int num=list.get(i); if (num==2) { list.remove(i); } } for (Integer integer : list) { System.out.println(integer);//13 }*/ //迭代器 Iterator<Integer>iterator =list.iterator(); while (iterator.hasNext()) { int num=iterator.next(); if (num==2) { iterator.remove(); } } for (Integer integer : list) { System.out.println(integer); } } }
3.linkedlist三种遍历
package 集合.LinkedList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class LinkedList_三种遍历 { public static void main(String[] args) { //声明集合 List<Integer> list=new LinkedList<Integer>(); //添加 list.add(1); list.add(2); list.add(3); //第一种用for get for (int i=0;i<list.size();i++){ int num=list.get(i); System.out.println(num);//123 } // 第二种 增强for for (Integer integer : list) { System.out.println(integer);//123 } //第三种 迭代器(推荐) Iterator<Integer> iterable=list.iterator(); while (iterable.hasNext()){ int num=iterable.next(); System.out.println(num);//123 } } }
总结
(1)List:元素有序 可重复
(2)ArrayList:底层数据结构是数组,查询快,增删慢,线程不安全,效率高,可以存储重复元素
(3)LinkedList底层数据结构是链表,查询慢,增删快,线程不安全,效率高,可以存储重复元素
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算