1.ArrayList
编程的时候如果要存储多个数据,使用长度固定的数组存储格式,不一定满足我们的需求,更适应不了变化的需求,那么,此时该如何选择呢?
集 合
集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以发生改变集合类有很多,目前我们先学习一个:ArrayList
2.ArrayList构造方法和添加方法
java
package Base_one.Base_009;
import java.util.ArrayList;
public class ArrayListDemo1 {
public static void main(String[] args) {
// ArrayList<String> array = new ArrayList<>();
ArrayList<String> array = new ArrayList<String>();
System.out.println(array); //[]
System.out.println(array.add("hello"));
System.out.println(array);
array.add("world");
System.out.println(array);
array.add(1,"javase");
array.add(3,"javaee");
System.out.println(array);
array.add(5,"sed"); // .IndexOutOfBoundsException 集合索引越界
}
}
3.ArrayList 存储字符串并遍历
java
package Base_one.Base_009;
import java.util.ArrayList;
public class ArrayListTest1 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("e");
System.out.println(arrayList);
// 遍历
for (int i=0; i<arrayList.size();i++){
String s = arrayList.get(i);
System.out.println(s);
}
}
}
4. ArrayList 集合常用方法
java
package Base_one.Base_009;
import java.util.AbstractList;
import java.util.ArrayList;
public class ArrayListDemo2 {
public static void main(String[] args) {
AbstractList<String> arr = new ArrayList<String>();
arr.add("a");
arr.add("b");
arr.add("c");
System.out.println(arr);
System.out.println(arr.remove(0));
System.out.println(arr.remove("b"));
System.out.println(arr);
System.out.println(arr.set(0,"e"));
System.out.println(arr);
System.out.println(arr.get(0));
System.out.println(arr.size());
}
}
5. 存储学生对象并遍历
java
package Base_one.Base_009;
public class Studet {
private String name;
private int age;
public Studet(){}
public Studet(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;
}
}
java
package Base_one.Base_009;
import java.util.AbstractList;
import java.util.ArrayList;
public class StudentTest {
public static void main(String[] args) {
// 创建集合对象
ArrayList<Studet> array = new ArrayList<Studet>();
// 创建学生对象
Studet s1 = new Studet("Tom",18);
Studet s2 = new Studet("Alo",20);
Studet s3 = new Studet("Tina",22);
// 添加学生对象到集合中
array.add(s1);
array.add(s2);
array.add(s3);
System.out.println(array); // 输出的都是地址了
for(int i= 0;i< array.size();i++){
Studet s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
}
6.升级版本
java
package Base_one.Base_009;
public class Studentpro {
private String name;
private String age;
public Studentpro() {
}
public Studentpro(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;
}
}
java
package Base_one.Base_009;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentProTest {
public static void main(String[] args) {
ArrayList<Studentpro> array = new ArrayList<Studentpro>();
Scanner sc = new Scanner(System.in);
System.out.println("输入你要添加几个学生对象:");
int cout = sc.nextInt();
for (int i = 0;i<cout;i++){
System.out.println(String.format("第%d个对象",i));
addStudentObject(array);
}
// 遍历输出
for (int i = 0;i<array.size();i++){
Studentpro s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
// 定义方法 返回值void类型 参数类型为ArrayList
public static void addStudentObject(ArrayList<Studentpro> array){
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = sc.nextLine();
System.out.println("请输入年龄");
String age = sc.nextLine();
Studentpro s = new Studentpro();
s.setName(name);
s.setAge(age);
array.add(s);
}
}