ArrayList 集合的应用,arraylist集合应用
分享于 点击 36101 次 点评:25
ArrayList 集合的应用,arraylist集合应用
1、
定义学生类(Student)保存学生信息,学生信息包括学号(id)、姓名(name)、年龄(age),使用ArrayList 来保存学生信息,并迭代输出。/
2、
修改学员信息,根据输入的学号来进行修改(存入的学号是唯一的),修改后将最新的学员信息输出,如图-1。.如果没有该学员,则提示信息
3、
实现删除学员信息,根据输入的学号来进行删除(存入的学号是唯一的),删除后将最新的学员信息输出。如果没有该学员,则提示信息。
package com.jredu.ch04;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayList2 {
private static Student student;
public static void main(String[] args) {
ArrayList<Student> arrayList = new ArrayList<Student>();
System.out.println("****录入学员信息,当录入学号为0时结束录入****");
Scanner input = new Scanner(System.in);
int id;
String name;
int age;
do{
System.out.print("请输入学号:");
id = input.nextInt();
if(id == 0){
System.out.println("结束录入!");
//input.close();
break;
}else{
System.out.print("请输入姓名:");
name = input.next();
System.out.print("请输入年龄:");
age = input.nextInt();
Student student = new Student(id,name,age);
arrayList.add(student);
}
}while(id!=0);
//输出学员信息
Student.showInfo(arrayList);
//第二题:修改学员信息
System.out.print("请输入需要修改的学员学号:");
id = input.nextInt()-1;
if(contains(id)){
System.out.print("请输入需要修改的学员年龄:");
age = input.nextInt();
System.out.print("请输入需要修改的学员姓名:");
name = input.next();
student = new Student(id+1,name,age);
arrayList.set(id,student);
Student.showInfo(arrayList);
}else{
System.out.println("没有该学生!");
}
//第三题:删除学员信息
System.out.print("请输入需要删除的学员学号:");
id = input.nextInt()-1;
if(contains(id)){
arrayList.remove(id);
System.out.println("学员的信息如下:");
System.out.println("学号\t姓名\t年龄");
Student.showInfo(arrayList);
}else{
System.out.println("没有该学生!");
}
input.close();
}
private static boolean contains(int id) {
return true;
}
}
相关文章
- 暂无相关文章
用户点评