概述
package Java_project_1;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Java_project_1 {
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);
System.out.println(scores);
for(int i=scores.size()-1;i>=0;--i){
if(scores.get(i)<80){
scores.remove(i);
}
}
System.out.println(scores);
}
}
package Java_project_1;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Java_project_1 {
public static void main(String[] args) {
Movie m1=new Movie("《肖申克的救赎》",9.7,"罗宾斯");
Movie m2=new Movie("《霸王别姬》",9.7,"张国荣、张丰毅");
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("---------------");
}
}
}
Student类:
package Java_project_1;
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 Java_project_1;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Java_project_1 {
public static void main(String[] args) {
ArrayList<Student>students=new ArrayList<Student>();
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("学号ttt名称tt年龄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;
}
}
最后
以上就是愤怒铅笔为你收集整理的ArrayList遍历删除、存对象、搜索的全部内容,希望文章能够帮你解决ArrayList遍历删除、存对象、搜索所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复