java:对象数组;Collection集合及常用方法,迭代器及增强for循环;List集合特点及方法,ArrayList使用及底层原理,数据结构,LinkedList使用及底层原理

本文主要是介绍java:对象数组;Collection集合及常用方法,迭代器及增强for循环;List集合特点及方法,ArrayList使用及底层原理,数据结构,LinkedList使用及底层原理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 对象数组

1 对象数组

案例需求
创建一个长度为3的数组,存入三个女朋友对象,假设女朋友的属性为:姓名和年龄。

代码实现

女朋友类

public class GirlFriend {private String name;private int age;public GirlFriend() {}public GirlFriend(String name, int age) {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;}
}

测试类

public class GirlFriendTest {public static void main(String[] args) {//需求:创建一个长度为3的数组,存入三个女朋友对象,假设女朋友的属性为:姓名和年龄。//1.创建一个数组。GirlFriend[] arr = new GirlFriend[3];//2.创建三个GirlFriend的对象GirlFriend gf1 = new GirlFriend("小诗诗",18);GirlFriend gf2 = new GirlFriend("小丹丹",17);GirlFriend gf3 = new GirlFriend("小惠惠",16);//3.把三个GirlFriend的对象存入到数组当中arr[0] = gf1;arr[1] = gf2;arr[2] = gf3;//4.遍历数组for (int i = 0; i < arr.length; i++) {GirlFriend gf = arr[i];System.out.println(gf.getName() + "---" + gf.getAge());}}
}

2 对象数组的内存图

对象数组,数组中保存的每一个对象的内存地址值
在这里插入图片描述

2 Collection集合

1 集合和数组的区别

  • 数组的长度是固定的,集合的长度是可变的
  • 数组既可以存储基本数据类型,也可以存储引用数据类型.集合只能存储引用数据类型(如果存储基本数据类型,需要使用基本数据类型对应的包装类
  • 基本类型对应的包装类
    在这里插入图片描述
    2 集合体系结构
  • 集合体系结构
    在这里插入图片描述
    3 Collection常用方法
  • 常用方法
    在这里插入图片描述
  • 示例代码
public class MyCollection1 {public static void main(String[] args) {
//        boolean add(E e)            添加元素
//        boolean remove(Object o)    从集合中移除指定的元素
//        void clear()                清空集合
//        boolean contains(Object o)  判断集合中是否存在指定的元素
//        boolean isEmpty()           判断集合是否为空
//        int size()                  集合的长度,也就是集合中元素的个数//method1();//method2();//method3();//method4();//method5();//method6();method7();}private static void method7() {//1,创建集合对象Collection<String> list = new ArrayList<>();//2.获取集合的长度int size1 = list.size();System.out.println(size1);//0//3.添加元素list.add("aaa");list.add("bbb");list.add("ccc");list.add("ccc");list.add("ccc");//4.获取集合的长度int size2 = list.size();System.out.println(size2);//5}private static void method6() {//1,创建集合对象Collection<String> list = new ArrayList<>();//2.判断集合是否为空boolean empty1 = list.isEmpty();System.out.println(empty1);//true//3.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//4.再次判断集合是否为空boolean empty2 = list.isEmpty();System.out.println(empty2);//false//5.调用一下clear方法来清空集合list.clear();//6.再次判断集合是否为空boolean empty3 = list.isEmpty();System.out.println(empty3);//true}private static void method5() {//1,创建集合对象Collection<String> list = new ArrayList<>();//2.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//3.判断集合中是否存在指定的元素boolean result1 = list.contains("ccc");System.out.println(result1);//true//4.判断集合中是否存在指定的元素boolean result2 = list.contains("ddd");System.out.println(result2);//false}private static void method4() {//1,创建集合对象Collection<String> list = new ArrayList<>();//2.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//3.打印集合System.out.println("清空前" + list);//[aaa, bbb, ccc]//4.清空集合list.clear();//5.打印集合System.out.println("清空后" + list);//集合里面什么都没有了.}private static void method3() {//1,创建集合对象Collection<String> list = new ArrayList<>();//2.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//3.打印集合System.out.println("删除前" + list);//[aaa, bbb, ccc]//4.删除dddboolean result = list.remove("ddd");System.out.println(result);//false --- 表示本次删除失败//5.打印集合System.out.println("删除后" + list);//[aaa, bbb, ccc]}private static void method2() {//1,创建集合对象Collection<String> list = new ArrayList<>();//2.打印集合System.out.println("添加前" + list);//3.添加元素boolean result1 = list.add("aaa");boolean result2 = list.add("bbb");boolean result3 = list.add("ccc");System.out.println(result1);//trueSystem.out.println(result2);//trueSystem.out.println(result3);//true//list.add(new GirlFriend());//4.打印集合System.out.println("添加后" + list);}private static void method1() {//1.如何创建集合对象//Collection list = new Collection();//错误的,Collection是一个接口,不能直接创建对象//jdk7以前的写法Collection<String> list1 = new ArrayList<String>();//jdk7省略Collection<String> list2 = new ArrayList<>();}
}

4 遍历集合-迭代器

  • 迭代器接口: Iterator
  • 获取迭代器接口实现类对象: Collection集合中的Iterator()方法
  • Iterator接口常用方法
    boolean hasNext() 判断迭代器中是否还有元素
    E next() 获取元素
  • 示例代码
public class MyCollection2 {public static void main(String[] args) {Collection<String> list = new ArrayList<>();//添加元素list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");list.add("e");list.add("e");list.add("e");list.add("e");list.add("e");list.add("e");//获取迭代器对象//默认指向集合的0索引位置Iterator<String> it = list.iterator();/* //hasNext --- 判断当前指向的位置是否有元素可以被取出System.out.println(it.hasNext());//true//next --- 获取当前位置的元素,把迭代器往后移动一个位置System.out.println(it.next());//aSystem.out.println(it.next());//bSystem.out.println(it.next());//cSystem.out.println(it.next());//dSystem.out.println(it.next());//eSystem.out.println(it.next());*/while(it.hasNext()){String element = it.next();System.out.println(element);}}
}

5 遍历集合-迭代器原理图解

  • Iterator iterator() : 创建迭代器对象,默认指向当前集合的0索引
  • boolean hasNext(): 判断当前位置是否有元素可以被取出
  • E next(): 获取当前位置的元素,并将迭代器对象移向下一个索引位置
  • hasNext()方法判断是否还有元素
    在这里插入图片描述
  • next() 方法获取元素
    在这里插入图片描述
    6 遍历集合- 增强for循环
  • 他是JDK5之后出现的,其内部原理是一个Iterator迭代器
  • 实现Iterator接口的类才可以使用迭代器和增强for(可以遍历数组和集合)
  • 定义格式
for(数据类型 变量名 : 数组或集合对象){循环体;
}
  • 示例代码
public class MyCollection3 {public static void main(String[] args) {Collection<String> list = new ArrayList<>();//添加元素list.add("a");list.add("b");list.add("c");list.add("d");//使用增强for进行遍历for(String str : list){//str依次表示集合中的每一个元素System.out.println(str);}}
}

7 集合练习-集合中保存学生对象并遍历

  • 案例需求: 创建集合对象,保存多个学生对象并遍历
  • 代码实现
    Student 类
public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {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;}
}

测试类

public class MyCollection5 {public static void main(String[] args) {//创建Collection集合对象Collection<Student> list = new ArrayList<>();//创建学生对象Student s1 = new Student("西门学吹牛",23);Student s2 = new Student("西门学吹水",25);Student s3 = new Student("aaa",29);//把学生添加到集合list.add(s1);list.add(s2);list.add(s3);//遍历集合(迭代器)Iterator<Student> it = list.iterator();while(it.hasNext()){Student stu = it.next();System.out.println(stu.getName() + "---" + stu.getAge());}System.out.println("=======================");//遍历集合(增强for)for (Student student : list) {System.out.println(student.getName() + "---" + student.getAge());}}
}

3 List集合

1 List集合的特点

  • 元素存取有序
  • 可以存储重复元素
  • 有索引

2 List特有方法

  • 常用方法
    在这里插入图片描述

  • 示例代码

public class MyList1 {public static void main(String[] args) {//void add(int index,E element)     在此集合中的指定位置插入指定的元素//E remove(int index)               删除指定索引处的元素,返回被删除的元素//E set(int index,E element)        修改指定索引处的元素,返回被修改的元素//E get(int index)                  返回指定索引处的元素//1.创建集合对象List<String> list = new ArrayList<>();//2.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//3.获取0索引上的元素String element = list.get(0);//System.out.println(element);//aaa//针对于List集合,又多了一种遍历方式.普通for循环for (int i = 0; i < list.size(); i++) {String s = list.get(i);System.out.println(s);}}private static void method3() {//1.创建集合对象List<String> list = new ArrayList<>();//2.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//3.修改元素//修改指定索引处的元素.返回被修改的元素.String s = list.set(0, "qwer");System.out.println(s);//aaa//4.打印集合System.out.println(list);//[qwer, bbb, ccc]}private static void method2() {//1.创建集合对象List<String> list = new ArrayList<>();//2.添加元素list.add("aaa");list.add("bbb");list.add("ccc");//3.删除元素//根据指定的索引进行删除,会把删除的元素进行返回.String s = list.remove(0);System.out.println(s);//aaa//4.打印集合System.out.println(list);//[bbb, ccc]}private static void method1() {//1.创建集合对象List<Integer> list = new ArrayList<>();//2.添加元素list.add(1);list.add(2);list.add(3);//利用List里面的特有方法添加元素void add(int index,E element)//在指定位置添加元素,原来的元素,依次往后移动一个位置.list.add(0,10);//3.打印集合System.out.println(list);//[10, 1, 2, 3]}
}

3 ArrayList基本使用

  • ArrayList集合是最常用的单列集合之一.可以使用Collection和List中的所有方法
  • 底层是数据结构实现的,查询快,增删慢
  • 示例代码
    Student类
public class Student {private String name;private int age;private int score;public Student() {}public Student(String name, int age, int score) {this.name = name;this.age = age;this.score = score;}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;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}
}

测试类

/*需求:创建一个ArrayList集合,存入三个学生对象。学生对象的属性为:姓名,年龄,考试成绩。打印考试成绩比平均分低的所有学生信息。
*/
public class MyArrayList1 {public static void main(String[] args) {//1.写一个Student类.属性:name age score//2.创建一个ArrayList集合ArrayList<Student> list = new ArrayList<>();//3.创建学生对象Student s1 = new Student("张三",18,100);Student s2 = new Student("lisi",20,60);Student s3 = new Student("王五",19,80);//4.把学生对象添加到集合中list.add(s1);list.add(s2);list.add(s3);//5.求平均分int sum = 0;for (Student student : list) {//student依次表示集合中的每一个学生对象sum = sum + student.getScore();}//当循环结束之后,就表示总分已经求出来了.int avg = sum / list.size();//System.out.println(avg);//6.遍历集合,获取每一个学生的成绩,打印比平均分低的学生信息for (Student student : list) {if(student.getScore() < avg){System.out.println(student.getName() + "---" + student.getAge() + "---" + student.getScore());}}}
}

4 数据结构-栈和队列
在这里插入图片描述
5 数据结构-数组和链表
在这里插入图片描述
在这里插入图片描述
** 6 ArrayList底层原理图解**

  • ArrayList底层是数组结构实现的,查询快增删慢
  • 初始会创建一个长度为10的数组,可以添加数据。如果添加满了,会自动扩容为原来的1.5倍,将原数组的数据拷贝到新数组中,剩余位置继续添加新的元素!
    在这里插入图片描述
    ** 7 LinkedList基本使用**
  • LinkedList集合底层是链表结构实现的,查询慢增删快
  • 示例代码
public class MyLinkedList1 {public static void main(String[] args) {//使用LinkedList完成存储字符串并用三种方式进行遍历//1.创建集合LinkedList<String> list = new LinkedList<>();//2,添加元素list.add("钢门吹雪");list.add("西域狂鸭");list.add("西门吹牛");list.add("洗浴K歌一条龙");//3.遍历(迭代器 --- 所有的单列集合都可以使用的)Iterator<String> it = list.iterator();while(it.hasNext()){String element = it.next();System.out.println(element);}System.out.println("==================");//遍历(增强for --- 所有的单列集合还有数组)for (String s : list) {System.out.println(s);}System.out.println("==================");for (int i = 0; i < list.size(); i++) {String element = list.get(i);System.out.println(element);}}
}

8 LinkedList特有方法

  • 特有方法
    在这里插入图片描述
  • 示例代码
public class MyLinkedList2 {public static void main(String[] args) {//1.创建集合对象LinkedList<String> list = new LinkedList<>();//2.添加元素list.add("a");list.add("b");list.add("c");System.out.println("删除前" + list);//3.removeFirst​()      从此列表中删除并返回第一个元素String first = list.removeFirst();System.out.println(first);//removeLast​()         从此列表中删除并返回最后一个元素String last = list.removeLast();System.out.println(last);System.out.println("删除后" + list);}private static void method2() {//1.创建集合对象LinkedList<String> list = new LinkedList<>();//2.添加元素list.add("a");list.add("b");list.add("c");//3.getFirst​ 返回此列表中的第一个元素String first = list.getFirst();System.out.println(first);//a//getLast​()   返回此列表中的最后一个元素String last = list.getLast();System.out.println(last);//c}private static void method1() {//1.创建集合对象LinkedList<String> list = new LinkedList<>();//2.添加元素list.add("a");list.add("b");list.add("c");//3.addFirst​(E e)   在该列表开头插入指定的元素list.addFirst("QQQ");//addLast(E e)      将指定的元素追加到此列表的末尾list.addLast("www");//4.打印集合System.out.println(list);}
}

** 9 LinkedList底层原理图解**

  • LinkedList底层采用双向链表结构实现
    在这里插入图片描述

这篇关于java:对象数组;Collection集合及常用方法,迭代器及增强for循环;List集合特点及方法,ArrayList使用及底层原理,数据结构,LinkedList使用及底层原理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/320899

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick