day07-常用类-ArrayList-使用步骤
This commit is contained in:
65
day07/src/com/inmind/arraylist03/Demo07.java
Normal file
65
day07/src/com/inmind/arraylist03/Demo07.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
常用类_ArrayList_使用步骤
|
||||||
|
1.导包java.util.ArrayList
|
||||||
|
2.通过构造方法创建对象ArrayList()
|
||||||
|
3.调用方法,add(E e)
|
||||||
|
|
||||||
|
arraylist的好处:1.长度可变 2.可以进行增删改查 3.arraylist可以存放任意引用数据类型
|
||||||
|
|
||||||
|
注意:ArrayList在使用的时候,大部分都是将一种类型的数据放在一个容器arraylist中
|
||||||
|
|
||||||
|
new GodNess()
|
||||||
|
new ArrayList<String>():表示创建一个只存放String类型的数据的集合
|
||||||
|
new ArrayList<GodNess>():表示创建一个只存放GodNess类型的数据的集合
|
||||||
|
|
||||||
|
泛型决定了集合中存放的数据类型!!!
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建arraylist对象
|
||||||
|
/*ArrayList lists = new ArrayList();//如果不写泛型,默认容器可以添加object,任意引用数据类型都可以添加到容器中
|
||||||
|
//往集合中添加内容
|
||||||
|
lists.add(1);//此处添加的是引用数据类型,不是基本数据类型,后面再说
|
||||||
|
lists.add(1.1);
|
||||||
|
lists.add(true);
|
||||||
|
lists.add("ddd");
|
||||||
|
Student s = new Student();
|
||||||
|
lists.add(s);*/
|
||||||
|
//创建一个只能添加字符串的集合
|
||||||
|
ArrayList<String> strLists = new ArrayList<>();
|
||||||
|
strLists.add("张三");
|
||||||
|
strLists.add("李四");
|
||||||
|
strLists.add("王五");
|
||||||
|
//保存是地址,打印的内容是 [张三, 李四, 王五],这里虽然输出了内容,是由于ArrayList类,重写了底层的功能实现(toString方法),在打印时不直接给地址,而是地址指向的内容
|
||||||
|
System.out.println(strLists);
|
||||||
|
|
||||||
|
//创建一个只能添加学生的集合
|
||||||
|
Student s1 = new Student();
|
||||||
|
Student s2 = new Student();
|
||||||
|
ArrayList<Student> studentList = new ArrayList<>();
|
||||||
|
studentList.add(s1);
|
||||||
|
studentList.add(s2);
|
||||||
|
System.out.println(studentList);//保存是地址
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void method(String[] args) {
|
||||||
|
//引用数据类型,作为数组的内容,比如String
|
||||||
|
String[] arr1 = new String[3];
|
||||||
|
String[] arr2 = new String[]{"hehe","heihei","haha"};
|
||||||
|
String[] arr3 = {"hehe","heihei","haha"};
|
||||||
|
|
||||||
|
//当数组定义完毕,我们还想要添加更多的数据??
|
||||||
|
// 能添加吗???不能,数组长度固定
|
||||||
|
//能减少吗???不能,数组长度固定
|
||||||
|
//为了解决以上的问题,有一个更好的容器,arraylist!!!
|
||||||
|
//arraylist的好处:1.长度可变 2.可以进行增删改查 3.arraylist可以存放任意引用数据类型
|
||||||
|
}
|
||||||
|
}
|
||||||
66
day07/src/com/inmind/arraylist03/Student.java
Normal file
66
day07/src/com/inmind/arraylist03/Student.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
private String name;
|
||||||
|
private int age = 18;
|
||||||
|
private int id;
|
||||||
|
private String gender;
|
||||||
|
private double score;
|
||||||
|
|
||||||
|
|
||||||
|
public Student(){
|
||||||
|
//对所有的属性设置默认值
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(String name){
|
||||||
|
//针对指定的属性赋值,其他的属性默认
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(String gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getScore() {
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(double score) {
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user