1:对象数组
(1)数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。

2:集合(Collection)
(1)集合的由来
我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组
而数组的长度固定,所以不适合做变化的需求,Java就提供了集合供我们使用。

(2)集合和数组的区别
A:长度区别
          数组固定
          集合可变
B:内容区别
         数组可以是基本类型,也可以是引用类型
         集合只能是引用类型
C:元素内容
        数组只能存储同一种类型
        集合可以存储不同类型(其实集合一般存储的也是同一种类型)

(3)集合的继承体系结构

由于需求不同,Java就提供了不同的集合类。这多个集合类的数据结构不同,但是它们都是要提供存储和遍历功能的,
我们把它们的共性不断的向上提取,最终就形成了集合的继承体系结构图。

Collection
     |--List
         |--ArrayList
         |--Vector
         |--LinkedList
   |--Set
        |--HashSet
        |--TreeSet

(4)Collection的功能概述
A:添加功能

boolean add(E e)添加一个元素
boolean addAll(Collection<? extends E> c)添加一个集合的元素

B:删除功能

void clear()移除所有元素
boolean remove(Object o)移除一个元素
boolean removeAll(Collection<?> c)移除一个集合的元素

C:判断功能

boolean contains(Object o)如果此 collection 包含指定的元素,则返回 true
boolean containsAll(Collection<?> c)如果此 collection 包含指定 collection 中的所有元素,则返回 true。 
boolean isEmpty()如果此 collection 不包含元素,则返回 true

D:获取功能

Iterator<E> iterator()返回在此 collection 的元素上进行迭代的迭代器

E:长度功能

int size()返回此 collection 中的元素数

F:交集(了解)

boolean retainAll(Collection<?> c)仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。

G:把集合转数组(了解)

<T> T[] toArray(T[] a)返回包含此 collection 中所有元素的数组
 import java.util.ArrayList;
import java.util.Collection; public class CollectioonDemo {
public static void main(String[] args) {
//创建对象
Collection c1 = new ArrayList(); //添加元素
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4"); Collection c2 = new ArrayList();
c1.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7"); //c1.clear();//移除所有元素
//System.out.println("remove:" + c1.remove("abc1"));//remove:true
//System.out.println("remove:" + c1.remove("abc"));//remove:false //判断集合中是否包含指定元素
//System.out.println("contains:" + c1.contains("abc2"));//contains:true
//System.out.println("contains:" + c1.contains("abc"));//contains:false //判断是否为空
//System.out.println("isEmpty:" + c1.isEmpty());//isEmpty:false //元素个数
//System.out.println("size:" + c1.size());//size:5 //添加一个集合的元素
//System.out.println("addAll:" + c1.addAll(c2));//addAll:true
//System.out.println("c1:" + c1);//c1:[abc1, abc2, abc3, abc4, abc4, abc5, abc6, abc7] //移除一个集合的元素 只用有一个元素被移除了就返回true
//System.out.println("removeAll:" + c1.removeAll(c2)); //只有包含所有的元素才叫包含
//System.out.println("containsAll:" + c1.containsAll(c2)); //交集
/*
* A对B做交集,最终的结果保存在A中,B不变
* 返回值表示A是否发生过变化
*/
System.out.println("retainAll:" + c1.retainAll(c2));
System.out.println("c1:" + c1);
System.out.println("c2:" + c2); } }

(5)Collection集合的遍历
A:把集合转数组(了解)

实例1

 import java.util.ArrayList;
import java.util.Collection; public class CollectioonDemo2 {
//集合变数组 实现集合的遍历
public static void main(String[] args) {
Collection c = new ArrayList(); c.add("hello");//Object obj = "hello" 向上转型
c.add("world");
c.add("java"); //集合c转数组
Object[] objs = c.toArray();
for(int x = 0;x < objs.length;x++){
System.out.println(objs[x]);
//object没有length()方法,必须将元素还原成字符串;向下转型
String s = (String) objs[x];
System.out.println(s+ "-----"+s.length());
}
} /*
* hello
hello-----5
world
world-----5
java
java-----4 */ }

实例2

 public class Student {
private String name; private int age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }
 import java.util.ArrayList;
import java.util.Collection; public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList(); //创建学生对象
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 25);
Student s3 = new Student("王五", 26); //将学生对象添加到集合
c.add(s1);
c.add(s2);
c.add(s3); //集合转化为数组
Object[] obj = c.toArray(); //遍历数组
for(int i = 0; i < obj.length; i++){
Student s = (Student)obj[i];
System.out.println(s.getName() + "-------" + s.getAge());
} } }

B:迭代器(集合专用方式)

实例1

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class IteratorDemo {
public static void main(String[] args) { Collection c = new ArrayList(); c.add("java");
c.add("hello");
c.add("word");
c.add("hi"); Iterator it = c.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);
} } }

实例2

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class ColectionTest {
public static void main(String[] args) { Collection c = new ArrayList(); Student s1 = new Student("貂蝉",25);
Student s2 = new Student("小乔",16);
Student s3 = new Student("黄月英",20);
Student s4 = new Student();
s4.setName("大桥");
s4.setAge(26); c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(new Student("孙尚香",18));//匿名对象 Iterator it = c.iterator();
while (it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName() + "---" + s.getAge());
} } }
 public class Student {
private String name; private int age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() { return "Student [name = " + name + ", age = " + age +" ]";
} }

(6)迭代器
A:是集合的获取元素的方式。
B:是依赖于集合而存在的。
C:迭代器的原理和源码。
a:为什么定义为了一个接口而不是实现类?
b:看了看迭代器的内部类实现。

(7)Collection集合的案例(遍历方式 迭代器)
集合的操作步骤:
A:创建集合对象
B:创建元素对象
C:把元素添加到集合
D:遍历集合

A:存储字符串并遍历

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; /*
* 存储字符串并遍历
* 步骤:
* 1.创建集合对象
* 2.创建字符串对象
* 3.把字符串对象添加到集合
* 4.遍历集合
*/
public class CollectionTest {
public static void main(String[] args) {
//1.创建集合对象
Collection c = new ArrayList();
/*
* 创建字符串对象
* 把字符串对象添加到集合
*/
c.add("张三");
c.add("李四");
c.add("王五");
c.add("刘二");
//通过迭代器遍历集合
Iterator it = c.iterator();
//通过迭代器对象的hashNext()方法判断有没有元素
while(it.hasNext()){
//通过迭代器对象的next()方法获取元素
String s = (String) it.next();
System.out.println(s);
}
} }

B:存储自定义对象并遍历

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; /*
* 存储自定义对象并遍历(Student name age)
* 步骤:
* 1.创建学生类
* 2.创建集合对象
* 3.创建学生对象
* 3.把学生对象添加到集合
* 4.遍历集合
*/
public class CollectionTest {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList(); //创建学生对象
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 21);
Student s3 = new Student("王五", 12);
//通过getset方法赋值
Student s4 = new Student();
s4.setName("刘二");
s4.setAge(22); //把学生对象添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(new Student("小儿", 10));//匿名对象 //通过迭代器遍历集合
Iterator it = c.iterator();
//通过迭代器对象的hashNext()方法判断有没有元素
while(it.hasNext()){
//通过迭代器对象的next()方法获取元素
Student s = (Student) it.next();
System.out.println(s.getName()+ "-----" + s.getAge());
}
} }
 public class Student {
private String name;
private int age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

3:集合(List)
(1)List是Collection的子接口
特点:有序(存储顺序和取出顺序一致),可重复。

存储字符串并遍历:

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; //List集合存储字符串并遍历
public class ListDemo {
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList(); //创建字符串并添加到集合
list.add("hello");
list.add("world");
list.add("java"); //遍历集合
Iterator it = list.iterator();
while(it.hasNext()){
String s = (String) it.next();
System.out.println(s);
}
} }

测试有序可重复:

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /*
* List集合特点:有序(存储和取出的元素一直),可重复的
*/
public class ListDemo {
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList(); //创建字符串并添加到集合
list.add("hello");
list.add("world");
list.add("java");
list.add("java");
list.add("Linux");
list.add("java");
list.add("Linux"); //遍历集合
Iterator it = list.iterator();
while(it.hasNext()){
String s = (String) it.next();
System.out.println(s);
/*
* hello
world
java
java
Linux
java
Linux
*/
}
} }

存储自定义对象并遍历:

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /*
* 存储自定义对象并遍历
*/
public class ListDemo {
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList(); //创建学生对象
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 24); //将学生对象添加到集合
list.add(s1);
list.add(s2);
list.add(s3); //遍历集合
Iterator it = list.iterator();
while(it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.getName() + "-------" + s.getAge());
}
} }
 public class Student {
private String name;
private int age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

(2)List的特有功能
A:添加功能

void add(int index, Object element):在指定位置添加元素
B:删除功能

Object remove(int index):根据索引删除元素,返回被删除的元素
C:获取功能

Object get (int index):获取指定位置的元素
D:列表迭代器功能

ListItertor listIterator():list集合特有的迭代器
E:修改功能

 Object set (int index,Object element):根据索引修改元素,返回被修改的元素

 public class ListDemo2 {

     public static void main(String[] args) {
//创建集合对象
List list = new ArrayList(); //添加元素
list.add("hello");
list.add("world");
list.add("java"); //System.out.println("list:" + list);
/*
* list:[hello, world, java]
*/ //添加功能
//list.add(1, "linux");
//System.out.println("list:" + list);
/*
* list:[hello, linux, world, java]
*/
//获取功能
//System.out.println("get:" + list.get(2));
//get:java //删除功能
//System.out.println("remove:" + list.remove(1));
//remove:world //修改功能
System.out.println("set:" + list.set(1, "javaee"));
System.out.println("list:" + list);
/*
* set:world
list:[hello, javaee, java]
*/ }
}

(3)List集合的特有遍历功能
A:由size()和get()结合。
B:代码演示

 import java.util.ArrayList;
import java.util.List; public class ListDemo1 {
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList(); //集合中添加元素
list.add("hello");
list.add("world");
list.add("java"); //遍历输出 list集合的特有遍历size()和get()结合
for(int x = 0; x < list.size(); x++){
String s = (String) list.get(x);
System.out.println(s);
}
}
}

自定义学生对象,两种遍历方式(迭代器和普通for)

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /*
* 存储自定义对象,使用普通for 循环(get()size()方法结合)
*/
public class ListDemo1 {
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList(); //创建学生对象
Student s1 = new Student("张三", 12);
Student s2 = new Student("李四", 11);
Student s3 = new Student("王五", 20); //学生对象添加到集合
list.add(s1);
list.add(s2);
list.add(s3); //迭代器遍历
Iterator it = list.iterator();
while(it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.getName() + "----" + s.getAge());
}
System.out.println("-----------");
//遍历输出 list集合的特有遍历size()和get()结合
for(int x = 0; x < list.size(); x++){
Student s = (Student) list.get(x);
System.out.println(s.getName() + "----" + s.getAge());
}
}
}

(4)列表迭代器的特有功能;(了解)
可以逆向遍历,但是要先正向遍历,所以无意义,基本不使用。

(5)并发修改异常
A:出现的现象
迭代器遍历集合,集合修改集合元素
B:原因
迭代器是依赖于集合的,而集合的改变迭代器并不知道。
C:解决方案
a:迭代器遍历,迭代器修改(ListIterator)
元素添加在刚才迭代的位置
b:集合遍历,集合修改(size()和get())
元素添加在集合的末尾

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator; public class ListDemo2 { public static void main(String[] args) {
//创建List集合对象
List list = new ArrayList(); //添加元素
list.add("hello");
list.add("world");
list.add("java"); //迭代器遍历
// Iterator it = list.iterator();
// while(it.hasNext()){
// String s = (String) it.next();
// if("world".equals(s)){
// list.add("javaee");
// }
// }
/*
* ConcurrentModificationException
*/ //1.迭代器遍历元素,迭代器修改元素
//Iterator迭代器没有添加功能,使用它的子接口ListIterator
ListIterator lit = list.listIterator();
while(lit.hasNext()){
String s = (String)lit.next();
if("world".equals(s)){
list.add("javaee");
}
}//list:[hello, world,javaee, java] //2.集合遍历元素,集合修改元素(普通for)
// for(int x = 0; x < list.size(); x++){
// String s = (String) list.get(x);
// if("world".equals(s)){
// list.add("javaee");
// }
// }
System.out.println("list:" + list);
//list:[hello, world, java, javaee]
}
}

(6)常见数据结构
A:栈 先进后出
B:队列 先进先出
C:数组 查询快,增删慢
D:链表 查询慢,增删快

(7)List的子类特点
ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。

到底使用谁呢?看需求?
分析:
要安全吗?
要:Vector(即使要,也不使用这个)
不要:ArrayList或者LinkedList
查询多;ArrayList
增删多:LinkedList

什么都不知道,就用ArrayList。

Java中的集合详解及代码测试的更多相关文章

  1. Java中日志组件详解

    avalon-logkit Java中日志组件详解 lanhy 发布于 2020-9-1 11:35 224浏览 0收藏 作为开发人员,我相信您对日志记录工具并不陌生. Java还具有功能强大且功能强 ...

  2. java中的注解详解和自定义注解

    一.java中的注解详解 1.什么是注解 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据.所以,可以说注解就是源代码的元数据.比如,下面这段代码: @Override public Str ...

  3. [转载]java中import作用详解

    [转载]java中import作用详解 来源: https://blog.csdn.net/qq_25665807/article/details/74747868 这篇博客讲的真的很清楚,这个作者很 ...

  4. Java中dimension类详解

    Java中dimension类详解 https://blog.csdn.net/hrw1234567890/article/details/81217788

  5. JAVA中Object类方法详解

    一.引言 Object是java所有类的基类,是整个类继承结构的顶端,也是最抽象的一个类.大家天天都在使用toString().equals().hashCode().waite().notify() ...

  6. Java中反射机制详解

    序言 在学习java基础时,由于学的不扎实,讲的实用性不强,就觉得没用,很多重要的知识就那样一笔带过了,像这个马上要讲的反射机制一样,当时学的时候就忽略了,到后来学习的知识中,很多东西动不动就用反射, ...

  7. Java 中的泛型详解-Java编程思想

    Java中的泛型参考了C++的模板,Java的界限是Java泛型的局限. 2.简单泛型 促成泛型出现最引人注目的一个原因就是为了创造容器类. 首先看一个只能持有单个对象的类,这个类可以明确指定其持有的 ...

  8. Java中Map用法详解

    原文地址http://blog.csdn.net/guomutian911/article/details/45771621 原文地址http://blog.csdn.net/sunny2437885 ...

  9. Java中的多线程详解

    如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个话其 ...

随机推荐

  1. js 遍历数组取出字符串用逗号拼接

    var arr = [{"name":"hhh"},{"name":"dddd"}] //用js function ge ...

  2. React Native 之项目的启动

    运行项目有两种方法 1. 到根目录,执行 react-native run-ios 命令 会开启一个本地服务,加载jsbundle文件,然后是去index.js文件 import {AppRegist ...

  3. ipcloud上传裁切图片

    主页: <!doctype html> <html> <head> <meta charset="utf-8"> <meta ...

  4. 二、angular7的基础知识学习

    <p> hello works </p> <div *ngIf="isShow">我是测试内容</div> <p> &l ...

  5. 深入理解BFC和IFC

    1. 为什么会有BFC和IFC 首先要先了解两个概念:Box和formatting context: Box:CSS渲染的时候是以Box作为渲染的基本单位.Box的类型由元素的类型和display属性 ...

  6. 取得所有网卡的MAC地址,包括禁用的

    先在nuget包中添加System.Management.Automation引用. 然后下面就是代码了. using System;using System.Collections.ObjectMo ...

  7. ELK实时日志分析平台环境部署--完整记录(ElasticSearch+Logstash+Kibana )

    https://blog.csdn.net/oLevin/article/details/81020794

  8. AtomicInteger 源码分析

    AtomicInteger AtomicInteger 能解决什么问题?什么时候使用 AtomicInteger? 支持原子更新的 int 值. 如何使用 AtomicInteger? 1)需要被多线 ...

  9. Web高级 JavaScript中的数据结构

    复杂度分析 大O复杂度表示法 常见的有O(1), O(n), O(logn), O(nlogn) 时间复杂度除了大O表示法外,还有以下情况 最好情况时间复杂度 最坏情况时间复杂度 平均情况时间复杂度 ...

  10. github javascript相关项目star数排行榜(前30,截止2016.11.18):

    github javascript相关项目star数排行榜(前30,截止2016.11.18): 前端开源框架 TOP 100 前端 TOP 100:::::https://www.awesomes. ...