目录
1.ArrayList
集合和数组的优势对比:
- 长度可变
- 添加数据的时候不需要考虑索引,默认将数据添加到末尾
1.1ArrayList类概述
-
什么是集合
提供一种存储空间可变的存储模型,存储的数据容量可以发生改变
-
ArrayList集合的特点
底层是数组实现的,长度可以变化
-
泛型的使用
用于约束集合中存储元素的数据类型
集合中只能存入引用数据类型,对于接本数据类型要用包装类
1.2ArrayList类常用方法
1.2.1构造方法
方法名 | 说明 |
---|---|
public ArrayList() | 创建一个空的集合对象 |
1.2.2成员方法
方法名 | 说明 |
---|---|
public E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
public E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
public E get(int index) | 返回指定索引处的元素 |
public int size() | 返回集合中的元素的个数 |
public boolean remove(Object o) | 删除指定的元素,返回删除是否成功 |
public boolean add(E e) | 将指定的元素追加到此集合的末尾 |
public void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
1.2.3示例代码
public class ArrayListDemo02 {
public static void main(String[] args) {
//创建集合
ArrayList<String> array = new ArrayList<String>();
//添加元素
array.add("hello");
array.add("world");
array.add("java");
//public boolean remove(Object o):删除指定的元素,返回删除是否成功
// System.out.println(array.remove("world"));
// System.out.println(array.remove("javaee"));
//public E remove(int index):删除指定索引处的元素,返回被删除的元素
// System.out.println(array.remove(1));
//IndexOutOfBoundsException
// System.out.println(array.remove(3));
//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
// System.out.println(array.set(1,"javaee"));
//IndexOutOfBoundsException
// System.out.println(array.set(3,"javaee"));
//public E get(int index):返回指定索引处的元素
// System.out.println(array.get(0));
// System.out.println(array.get(1));
// System.out.println(array.get(2));
//System.out.println(array.get(3)); //?????? 自己测试
//public int size():返回集合中的元素的个数
System.out.println(array.size());
//输出集合
System.out.println("array:" + array);
}
}
1.3ArrayList存储字符串并遍历
1.3.1案例需求
创建一个存储字符串的集合,存储3个字符串元素,使用程序实现在控制台遍历该集合
1.3.2代码实现
/*
思路:
1:创建集合对象
2:往集合中添加字符串对象
3:遍历集合,首先要能够获取到集合中的每一个元素,这个通过get(int index)方法实现
4:遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现
5:遍历集合的通用格式
*/
public class ArrayListTest01 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//往集合中添加字符串对象
array.add("刘正风");
array.add("左冷禅");
array.add("风清扬");
//遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现
// System.out.println(array.size());
//遍历集合的通用格式
for(int i=0; i<array.size(); i++) {
String s = array.get(i);
System.out.println(s);
}
}
}
1.4ArrayList存储学生对象并遍历
1.4.1案例需求
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
1.4.2代码实现
/*
思路:
1:定义学生类
2:创建集合对象
3:创建学生对象
4:添加学生对象到集合中
5:遍历集合,采用通用遍历格式实现
*/
public class ArrayListTest02 {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<>();
//创建学生对象
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("风清扬", 33);
Student s3 = new Student("张曼玉", 18);
//添加学生对象到集合中
array.add(s1);
array.add(s2);
array.add(s3);
//遍历集合,采用通用遍历格式实现
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
}
1.5ArrayList存储学生对象并遍历升级版
1.5.1案例需求
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
学生的姓名和年龄来自于键盘录入
1.5.2代码实现
public class ArrayListTest {
public static void main(String[] args) {
//1.创建集合对象
ArrayList<Student> list = new ArrayList<>();
//2.键盘录入数据并添加到集合中
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
//创建学生对象
Student s = new Student();
//键盘录入学生信息
System.out.println("请输入第" + i + "个学生的姓名");
String name = sc.next();
System.out.println("请输入第" + i + "个学生的年龄");
int age = sc.nextInt();
//把学生信息赋值给学生对象
s.setName(name);
s.setAge(age);
//把学生对象添加到集合当中
list.add(s);
}
//遍历
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getName() + ", " + stu.getAge());
}
}
}
1.6 查找用户是否存在
需求:
1,main方法中定义一个集合,存入三个用户对象。
用户属性为:id,username,password
2,要求:定义一个方法,根据id查找对应的学生信息。
如果存在,返回true
如果不存在,返回false
代码示例:
package com.itheima.test4;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
//1.创建集合
ArrayList<User> list = new ArrayList<>();
//2.添加用户对象
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","1234");
User u3 = new User("heima003","wangwu","12345");
//3.添加元素
list.add(u1);
list.add(u2);
list.add(u3);
//3.根据id查找是否存在
//调方法
//如果调用本类中的方法,直接写方法名就可以。
//如果我要调用其他类中的方法,需要用对象去调用。
boolean flag = contains(list, "heima004");
System.out.println(flag);
}
//1.我要干嘛? 判断id在集合中是否存在
//2.需要什么? 集合 id
//3.是否需要继续使用?需要
//写在测试类中的方法,加static
//写在javabean类中的方法,不加static
public static boolean contains(ArrayList<User> list, String id){
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
String uid = u.getId();
if(uid.equals(id)){
return true;
}
}
//当集合里面所有的元素全部比较完毕了
//如果此时还不存在,才能返回false
return false;
}
}
1.7 查找用户的索引
需求:
1,main方法中定义一个集合,存入三个用户对象。
用户属性为:id,username,password
2,要求:定义一个方法,根据id查找对应的学生信息。
如果存在,返回索引
如果不存在,返回-1
代码示例:
package com.itheima.test5;
import com.itheima.test4.User;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
//1.创建集合
ArrayList<User> list = new ArrayList<>();
//2.添加用户对象
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","1234");
User u3 = new User("heima003","wangwu","12345");
//3.添加元素
list.add(u1);
list.add(u2);
list.add(u3);
//4.查询索引
int index = findIndex(list, "heima004");
System.out.println(index);
}
//1.我要干嘛?查询索引
//2.需要什么?集合 id
//3.是否需要继续使用 需要返回值
public static int findIndex(ArrayList<User> list, String id){
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
String uid = u.getId();
if(uid.equals(id)){
return i;
}
}
//如果循环结束还没有找到
return -1;
}
public static boolean contains(ArrayList<User> list, String id){
int index = findIndex(list, id);
if(index >= 0){
return true;
}else{
return false;
}
// return findIndex(list, id) >= 0;
}
}
2、包装类
针对八种基本数据类型定义相应的引用类型 — 包装类(封装类)
有了类的特点,就可以调用类中的方法,java才是真正的面向对象(面向对象主要体现类的特性)
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
java提供了八种基本数据类型的包装类,使得基本数据类型的变量具有类的特征
需要掌握:基本数据类型、包装类、String三者之间的相互转换
总结如下:
1.基本数据类型转换成包装类
方式一:使用包装类提供的方法去new对象,例如Integer num = new Integer(123);
方式二:JDK5.0之后新特性,自动装箱,例如 int num1 = 123; Integer num = num1;
2.包装类转换成基本数据类型
方式一:使用包装类提供的value()方法,例如 int num = Integer.value();
方式二:JDK5.0之后新特性,自动拆箱,例如Integer num1 = new Integer(123); int num = num1;
自此由于自动装箱和自动拆箱,基本数据类型和包装类之间的转换就比较容易
3.基本数据类型、包装类转换成String类型
方式一:使用连接运算,例如 System.out.println(123 + "");
方式二:调用String的valueOf(),例如 String str = String.valueOf(123);
4.String类型转换成基本数据类型、包装类
调用包装类中的parseXxx(String s)方法,例如 int num = Integer.parseInt(str1);
注意:boolean类型的转换
1.转换成包装类时 不区分大小写,只要不是true就是false
2.包装类转换成基本数据类型时 同上结论
2.1基本数据类型 ----> 包装类
-
调用包装类的构造器
@Test { int num1 = 10; //System.out.println(num1.toString());报错 Integer in1 = new Integer(num1); System.out.println(in1.toString()); Integer in2 = new Integer("123"); System.out.println(in1.toString()); //Integer in2 = new Integer("123abc");报异常 Float f1 = new Float(12.3f); Float f1 = new Float("12.3"); System.out.println(f1); System.out.println(f1); Boolean b1 = new Boolean(true); Boolean b2 = new Boolean("TrUe"); System.out.println(b2);//true Boolean b3 = new Boolean("true123"); System.out.println(b3);//false,不区分大小写,只要不是true就是false Order order = new Order(); System.out.println(Order.isMale);//false,基本数据类型 System.out.println(order.isFemale);//null,现在是类了,引用数据类型 } class Order{ boolean isMale; Boolean isFemale; }
2.2包装类 ---->基本数据类型
-
调用Xxx的xxxValue()方法
Integer in1 = new Integer(12); int i1 = in1.intValue(); System.out.println(i1);//12 Float f1 = new Float(12.3); float f2 = f1.floatValue(); System.out.println(f2 + 1);
-
JDK5.0新特性:自动装箱与自动拆箱
替代之前的包装类使用方法,不用再去new构造器了a
//自动装箱,基本数据类型 ----> 包装类 int num1 = 10; Integer in1 = num1; boolean b1 = true; Boolean b2 = b1; //自动拆箱 包装类 ---->基本数据类型 System.out.println(in1.toString()); int num2 = in1;
-
基本数据类型、包装类 ----> String类型
-
调用String的valueOf(Xxx xxx)
//方式一:连接运算 int num1 = 10; String str1 = num1 + ""; //方式二:调用String的valueOf(Xxx xxx) float f1 = 12.3f; String str2 = String.valueOf(f1);//"12.3" Double d1 = new Double(12.4); String str3 = String.valueOf(d1); System.out.println(str2); System.out.println(str3);//"12.4"
-
-
String类型 ----> 基本数据类型、包装类
-
调用包装类中的parseXxx(变量)方法
String str1 = "123"; //错误方法 //int num1 = (int)str1; //Integer in1 = (Integer)str1; int num2 = Integer.parseInt(str1); System.out.println(num2); String str2 = "true"; boolean b1 = Boolean.parseBoolean(str2); System.out.println(b1);
-
-
拓展:面试题
public class InterviewTest { public static void main(String[] args) { Object o1 = true ? new Integer(1) : new Double(2.0); System.out.println(o1);//1.0,条件运算符后面的数据类型会保持一致 Object o2; if(true) { o2 = new Integer(1); }else { o2 = new Double(2); } System.out.println(o2);//1,正常的if-else Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i == j);//false,==比的是地址值,new了两次 Integer m = 1; Integer n = 1; System.out.println(m == n);//true,Integer内部定义了IntegerCashe结构,IntegerCashe中定义了Integer[],保存了从-128到127范围内的整数。如果使用自动装箱的方式,给Integer赋值的范围在-128到127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率 Integer x = 128;//相当于new了一个Integer对象 Integer y = 128;//相当于new了一个Integer对象 System.out.println(x == y);//false } }