目录
- 集合概述
- 总结
- ArrayList集合快速入门
- 总结
- ArrayList对于泛型的支持
- 总结
- ArrayList常用API、遍历
- 集合案例
- 遍历并删除元素值
- 总结
- 存储自定义类型的对象
- 元素搜索
集合概述
总结
1、数组和集合的元素存储的个数问题?
- 数组定义后类型确定,长度固定
- 集合类型可以不固定,大小是可变的。
2、数组和集合适合的场景
- 数组适合做数据个数和类型确定的场景
- 集合适合做数据个数不确定,且要做增删元素的场景
ArrayList集合快速入门
package com.itheima.arraylist;
import java.util.ArrayList;
public class ArrayListDemo1 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("Java");
list.add("Java");
list.add("Mysql");
list.add("黑马");
list.add(23);
list.add(23.5);
list.add(false);
System.out.println(list.add('中'));
System.out.println(list);
list.add(1,"小陈");
System.out.println(list);
}
}
总结
1、ArrayList类如何创建集合对象的,如何添加元素?
- ArrayList list = new ArrayList();
- public boolean add(E e);
- public void add(int index,E element);
ArrayList对于泛型的支持
package com.itheima.arraylist;
import java.util.ArrayList;
public class ArrayListDemo2 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Mysql");
// list.add(23);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(23);
list2.add(100);
}
}
总结
1、怎么去统一ArrayList集合操作的元素类型?
使用泛型:<数据类型>
ArrayList list1 = new ArrayList<>();
ArrayList常用API、遍历
package com.itheima.arraylist;
import java.util.ArrayList;
public class ArrayListDemo3 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Java");
list.add("MySQL");
list.add("MyBatis");
list.add("HTML");
String e = list.get(3);
System.out.println(e);
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println(list);
String e2 = list.remove(2);
System.out.println(e2);
System.out.println(list);
System.out.println(list.remove("MyBatis"));
System.out.println(list);
ArrayList <String> list1 = new ArrayList<>();
list1.add("Java");
list1.add("王宝强");
list1.add("Java");
System.out.println(list1);
System.out.println(list1.remove("Java"));
System.out.println(list1);
String e3 = list1.set(0,"贾乃亮");
System.out.println(e3);
System.out.println(list1);
}
}
集合案例
遍历并删除元素值
package com.itheima.arraylist;
import java.util.ArrayList;
public class ArrayListTest4 {
public static void main(String[] args) {
ArrayList <Integer> scores = new ArrayList<>();
scores.add(98);
scores.add(77);
scores.add(66);
scores.add(89);
scores.add(79);
scores.add(50);
scores.add(100);
for(int i= 0; i<scores.size();i++){
int score = scores.get(i);
if(score<80){
scores.remove(i);
}
}
System.out.println(scores);
System.out.println("因为集合是动态变化的,删了之后,后面的会直接补上");
for(int i= 0; i<scores.size();i++){
int score = scores.get(i);
if(score<80){
scores.remove(i);
i--;
}
}
System.out.println(scores);
for(int i = scores.size()-1;i>=0;i--){
int score = scores.get(i);
if(score<80){
scores.remove(i);
}
}
System.out.println(scores);
}
}
总结
1、从集合中遍历元素,并筛选出元素删除它,应该怎么解决?
- 从集合后面遍历然后删除,可以避免漏掉元素。
存储自定义类型的对象
package com.itheima.arraylist;
public class Movie {
private String name;
private double score;
private String actor;
public Movie() {
}
public Movie(String name, double score, String actor) {
this.name = name;
this.score = score;
this.actor = actor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
}
package com.itheima.arraylist;
import java.util.ArrayList;
public class ArrayListTest5 {
public static void main(String[] args) {
Movie m1 = new Movie("肖生克的救赎",9.7,"罗宾斯");
Movie m2 = new Movie("霸王别姬",9.5,"张国荣");
Movie m3 = new Movie("阿甘正传",9.5,"汤姆.汉克斯");
ArrayList <Movie> movies = new ArrayList<>();
movies.add(m1);
movies.add(m2);
movies.add(m3);
System.out.println(movies);
for (int i = 0; i < movies.size(); i++) {
Movie m = movies.get(i);
System.out.println("电影名称:"+m.getName());
System.out.println("电影得分:"+m.getScore());
System.out.println("电影主演:"+m.getActor());
System.out.println("------------");
}
}
}
元素搜索
package com.itheima.arraylist;
public class Student {
private String studyNumber;
private String name;
private int age;
private String className;
public Student() {
}
public Student(String studyNumber, String name, int age, String className) {
this.studyNumber = studyNumber;
this.name = name;
this.age = age;
this.className = className;
}
public String getStudyNumber() {
return studyNumber;
}
public void setStudyNumber(String studyNumber) {
this.studyNumber = studyNumber;
}
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 String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
package com.itheima.arraylist;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListTest6 {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("20180302","叶孤城",23,"护理一班"));
students.add(new Student("20180303","东方不败",23,"推拿二班"));
students.add(new Student("20180304","西门吹雪",26,"临床四班"));
students.add(new Student("20180305","梅超风",26,"神经科2班"));
System.out.println("学号\t\t名称\t年龄\t班级");
for (int i = 0; i < students.size(); i++) {
Student s =students.get(i);
System.out.println(s.getStudyNumber()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getClassName());
}
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请您输入学号查询学生对象:");
String studyNumber = sc.next();
Student s = getStudentById(students,studyNumber);
if(s==null){
System.out.println("对不起,查无此人~~");
}else{
System.out.println("您查询的学生信息如下:");
System.out.println(s.getStudyNumber()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getClassName());
}
}
}
public static Student getStudentById(ArrayList<Student> students,String studyNumber){
for (int i = 0; i < students.size(); i++) {
Student s =students.get(i);
if(s.getStudyNumber().equals(studyNumber)){
return s;
}
}
return null;
}
}