A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是楠楠 于 2018-2-27 10:40 编辑

第2章 集合类之ArrayList

1.1 集合概述
A:我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象来体现的。
            为了方便对多个对象进行操作,我们就必须对这多个对象进行存储,而要想对多个对象进行存储,        就不能是一个基本的变量,而应该是一个容器类型的变量。

B:到目前为止,我们学习过了哪些容器类型的数据呢?
StringBuilder,数组。
         StringBuilder的结果只能是一个字符串类型,不一定满足我们的需求。
         所以,我们目前只能选择数组了,也就是我们前面学习过的对象数组。
         但是,数组的长度是固定的, 如果有时候元素的个数不确定的,我们无法定义出数组的长度,这个时候,java就提供了集合类供我们使用。
1.2 ArrayList集合
1.2.1 ArrayList添加新元素
1.2.1.1 案例代码:
[AppleScript] 纯文本查看 复制代码
package com.itheima_01;
 
import java.util.ArrayList;
 
/*
 * 为什么会出现集合类:
 * 我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象来体现的。
 * 为了方便对多个对象进行操作,我们就必须对这多个对象进行存储,而要想对多个对象进行存储,
 * 就不能是一个基本的变量,而应该是一个容器类型的变量。
 * 到目前为止,我们学习过了哪些容器类型的数据呢?StringBuilder,数组。
 * StringBuilder的结果只能是一个字符串类型,不一定满足我们的需求。
 * 所以,我们目前只能选择数组了,也就是我们前面学习过的对象数组。
 * 但是,数组的长度是固定的,适应不了变化的需求,那么,我们该如何选择呢?
 * 这个时候,java就提供了集合类供我们使用。
 *
 * 集合类的特点:
 * 长度可变。
 *
 * ArrayList<E>:
 * 大小可变数组的实现
 *
 * <E>:是一种特殊的数据类型,泛型。
 * 怎么用呢?
 * 在出现E的地方我们使用引用数据类型替换即可
 * 举例:ArrayList<String>,ArrayList<Student>
 *
 * 构造方法:
 * ArrayList()
 *
 * 添加元素:
 * public boolean add(E e):添加元素
 * public void add(int index,E element):在指定的索引处添加一个元素
 */
public class ArrayListDemo {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new  ArrayList<String>();
//add(E e):添加元素
array.add("hello");
array.add("world");
array.add("java");
//add(int index,E element):在指定的索引处添加一个元素
//array.add(1, "android");
System.out.println("array:"+array);
}
}

1.2.2 ArrayList删改查方法
A:获取元素
    public E get(int index):返回指定索引处的元素
B:集合长度
            public int size():返回集合中的元素的个数
C:删除元素
    public boolean remove(Object o):删除指定的元素,返回删除是否成功
    public E remove(int index):删除指定索引处的元素,返回被删除的元素
D:修改元素
public E set(int index,E element):修改指定索引处的元素,返回被修改的元素

1.2.2.1 案例代码三:
[AppleScript] 纯文本查看 复制代码
package com.itheima_01;
 
import java.util.ArrayList;
 
/*
 * 获取元素
 * public E get(int index):返回指定索引处的元素
 * 集合长度
 * public int size():返回集合中的元素的个数
 * 删除元素
 * public boolean remove(Object o):删除指定的元素,返回删除是否成功
 * public E remove(int index):删除指定索引处的元素,返回被删除的元素
 * 修改元素
 * public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
 */
public class ArrayListDemo2 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//添加元素
array.add("hello");
array.add("world");
array.add("java");
//public E get(int index):返回指定索引处的元素
//System.out.println("get:"+array.get(0));
//System.out.println("get:"+array.get(1));
//System.out.println("get:"+array.get(2));
//public int size():返回集合中的元素的个数
//System.out.println("size:"+array.size());
//public boolean remove(Object o):删除指定的元素,返回删除是否成功
//System.out.println("remove:"+array.remove("world"));//true
//System.out.println("remove:"+array.remove("world"));//false
//public E remove(int index):删除指定索引处的元素,返回被删除的元素
//System.out.println("remove:"+array.remove(0));
//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
System.out.println("set:"+array.set(1, "android"));
//输出
System.out.println("array:"+array);
}
}

1.2.3 ArrayList遍历
集合的遍历思想和数组的遍历思想相同
循环遍历容器,依次取出里面的元素即可
1.2.3.1 案例代码四:
[AppleScript] 纯文本查看 复制代码
package com.itheima_01;
import java.util.ArrayList;
 
/*
 * ArrayList集合的遍历
 * 通过size()和get()配合实现的
 */
public class ArrayListDemo3 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//添加元素
array.add("hello");
array.add("world");
array.add("java");
//获取元素
//原始做法
System.out.println(array.get(0));
System.out.println(array.get(1));
System.out.println(array.get(2));
System.out.println("----------");
for(int x=0; x<3; x++) {
System.out.println(array.get(x));
}
System.out.println("----------");
//如何知道集合中元素的个数呢?size()
for(int x=0; x<array.size(); x++) {
System.out.println(array.get(x));
}
System.out.println("----------");
//最标准的用法
for(int x=0; x<array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
}
}

1.3 ArrayList集合案例
1.3.1 ArrayList练习之存储字符串并遍历
   向集合中添加任意四个字符串,遍历集合,依次打印取出的字符串
1.3.1.1 案例代码:
[AppleScript] 纯文本查看 复制代码
package com.itheima_02;
import java.util.ArrayList;
 
/*
 * 存储字符串并遍历
 *
 * 分析:
 * A:创建集合对象
 * B:添加字符串元素
 * C:遍历集合
 */
public class ArrayListTest {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//添加字符串元素
array.add("向问天");
array.add("刘正风");
array.add("左冷禅");
array.add("风清扬");
//遍历集合
for(int x=0; x<array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
}
}

1.3.2 ArrayList练习之获取满足要求的元素
     给定一个字符串数组:{“张三丰”,“宋远桥”,“张无忌”,“殷梨亭”“张翠山”,“莫声谷”},将数组中的元素添加到集合中,并把所有姓张的人员打印到控制台上
1.3.2.1 案例代码六:
[AppleScript] 纯文本查看 复制代码
package com.itheima_02;
import java.util.ArrayList;
/*
 * 给定一个字符串数组:{“张三丰”,“宋远桥”,“张无忌”,“殷梨亭”,“张翠山”,“莫声谷”},将数组中的元素添加到集合中,并把所有姓张的人员打印到控制台上。
 *
 * 分析:
 * A:定义字符串数组
 * B:创建集合对象
 * C:遍历字符串数组,获取到每一个字符串元素
 * D:把获取到的字符串元素添加到集合
 * E:遍历集合
 * 要判断每一个字符串元素是否以"张"开头,如果是,就输出在控制台
 */
public class ArrayListTest2 {
public static void main(String[] args) {
//定义字符串数组
String[] strArray = {"张三丰","宋远桥","张无忌","殷梨亭","张翠山","莫声谷"};
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//遍历字符串数组,获取到每一个字符串元素
for(int x=0; x<strArray.length; x++) {
//把获取到的字符串元素添加到集合
array.add(strArray[x]);
}
//遍历集合
for(int x=0; x<array.size(); x++) {
String s = array.get(x);
//要判断每一个字符串元素是否以"张"开头,如果是,就输出在控制台
if(s.startsWith("张")) {
System.out.println(s);
}
}
}
}
1.3.3 ArrayList练习之存储自定义对象并遍历
A:自定义一个学生类,学生中有姓名和年龄属性,生成满参构造与空参构造
生成属性对应的getter/setter方法

B:在测试类中使用满参构造创建三个学生对象,然后将每个学生对象均添加到ArrayList集合中

C:遍历这个ArrayList集合,依次打印出每个学生的姓名和年龄
1.3.3.1 案例代码七:
[AppleScript] 纯文本查看 复制代码
package com.itheima_02;
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;
}
}

[AppleScript] 纯文本查看 复制代码
package com.itheima_02;
import java.util.ArrayList;
 
/*
 * 存储自定义对象并遍历
 *
 * 分析:
 * A:定义学生类
 * B:创建集合对象
 * C:创建学生对象
 * D:把学生对象作为元素添加到集合中
 * E:遍历集合
 */
public class ArrayListTest3 {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("林青霞",28);
Student s2 = new Student("张曼玉",30);
Student s3 = new Student("景甜",25);
Student s4 = new Student("柳岩",18);
//把学生对象作为元素添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍历集合
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
1.3.4 ArrayList练习之键盘录入数据存储并遍历
     创建一个Student类包含姓名和年龄属性
创建一个ArrayList集合
     向集合中添加三个Student对象Student对象中姓名和年龄的数据均来自与键盘录入
     最终遍历这个集合,取出Student对象以及里面属性的值
1.3.4.1 案例代码:
[AppleScript] 纯文本查看 复制代码
package com.itheima_03;
 
public class Student {
private String name;
private String age;
public Student() {
 
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}

[AppleScript] 纯文本查看 复制代码
package com.itheima_03;
 
import java.util.ArrayList;
import java.util.Scanner;
 
/*
 * 创建一个集合,存储学生对象,学生对象的数据来自于键盘录入,最后,遍历集合
 *
 * 注意:为了方便使用,我把学生类中的所有成员定义为String类型
 *
 * 分析:
 * A:定义学生类
 * B:创建集合对象
 * C:键盘录入数据,创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
 * D:把学生对象作为元素存储到集合中
 * E:遍历集合
 *
 */
public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
/*
//键盘录入数据,创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
Student s = new Student();
s.setName(name);
s.setAge(age);
//把学生对象作为元素存储到集合中
array.add(s);
*/
//为了提高代码的复用性,我把键盘录入数据给学生对象,并存储到集合中的动作用一个方法来实现
//调用方法
addStudent(array);
addStudent(array);
addStudent(array);
//遍历集合
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getName()+"---"+s.getAge());
}
}
/*
 * 两个明确:
 * 返回值类型:void
 * 参数列表:ArrayList<Student> array
 */
public static void addStudent(ArrayList<Student> array) {
//键盘录入数据,创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
Student s = new Student();
s.setName(name);
s.setAge(age);
//把学生对象作为元素存储到集合中
array.add(s);
}
}
更多
第一天

传智播客·黑马程序员郑州校区地址
河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话 0371-56061160/61/62
来校路线  地铁一号线梧桐街站A口出

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马