1. 集合基础
1.1 集合概述
编程的时候如果要存储多个数据,使用长度固定的数组存储格式,不一定满足我们的要求,更适应不了变化的需求,这时候就要用到集合
**集合的特点:**提供一种存储空间可变的存储模型,存储的数据容量可以发送改变
(集合类有很多,目前我们先学一个:ArrayList)
1.2 ArrayList构造方法和添加方法
package test1;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
//创建ArrayList<>对象
ArrayList<String> array = new ArrayList<>();
System.out.println(array.add("hello"));
array.add("hello");
array.add("world");
array.add("java");
System.out.println("array:" + array);
array.add(1, "javaae");
System.out.println("array:" + array);
}
}
1.3 ArrayList集合常用方法
package test1;
import java.util.ArrayList;
public class ArrayListDemo_01 {
public static void main(String[] args) {
//创建ArrayList<>对象
ArrayList<String> array = new ArrayList<>();
//添加元素
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));
//System.out.println(array.remove(3));错误:索引越界
//public E set(int index, E element):修改指定索引处的元素,返回被修改的元素
//System.out.println(array.set(1,"javaee"));
//public E set(int index, E element):返回指定索引处的元素
//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);
}
}
案例:存储字符串并遍历
package test1;
import java.util.ArrayList;
public class ArrayListDemo_02 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<>();
array.add("刘正风");
array.add("左冷禅");
array.add("风清扬");
// System.out.println(array.get(0));
// System.out.println(array.get(1));
// System.out.println(array.get(2));
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
}
}
案例:存储学生对象并遍历
package test1;
///学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
package test1;
import java.util.ArrayList;
public class ArrayListDemo_03 {
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("Student" + i + ":" + s.getName() + "." + s.getAge());
}
}
}
案例:存储学生对象并遍历
package test1;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListDemo_04 {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<>();
addStudent(array);
addStudent(array);
addStudent(array);
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
public static void addStudent(ArrayList<Student> array) {
//键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
Student s = new Student();
s.setName(name);
s.setAge(age);
array.add(s);
}
}