这是开始界面,供用户使用选择功能:
package com.cn.list;
import java.util.List;
import java.util.Scanner;
public class Start {
//private static List data;
public static void main(String args[]){
Mthod m=new Mthod();
Scanner in = new Scanner(System.in);
System.out.println("1.查询 2.删除");
int i = in.nextInt();
if(i!=1&&i!=2){
System.out.println("您输出的数值超出有效数值,请重新输入!!!!");
}
switch(i){
case 1:
{
System.out.println("请输入,要查询的名字!");
String name=in.next();
Student stu=m.findname(name);//调用findname方法的到集合的元素
System.out.println(stu);
// m.check();
}
break;
case 2:
//m.remove();
System.out.println("请输入,要删除的名字!");
String name=in.next();
List<Student> list = m.delete(name);//得到删除后的集合 stu.list将其转存到list中
System.out.println("删除成功,删除后的集合为:");
for(int j=0; j<list.size();j++){//遍历删除后的集合
System.out.println(list.get(j));
}
break;
}
}
}
创建对象:
package com.cn.list;
public class Student {
private String stuname;
private String stumajor;
private String stugrade;
private String direction;
public Student(String stuname,String stumajor,String stugrade,String direction){
this.stuname=stuname;
this.stumajor = stumajor;
this.stugrade=stugrade;
this.direction=direction;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStumajor() {
return stumajor;
}
public void setStumajor(String stumajor) {
this.stumajor = stumajor;
}
public String getStugrade() {
return stugrade;
}
public void setStugrade(String stugrade) {
this.stugrade = stugrade;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public String toString(){
return this.stuname+this.stumajor+this.stugrade+this.direction;
}
}
创建list集合,将我们查找的信息输进去:
package com.cn.list;
import java.util.ArrayList;
import java.util.List;
public class Stu {
//List<Student> list=new <>();
List<Student>list=new ArrayList<>();
public void getlist()//get list
{
Student stu1=new Student("蔡小莉","信管","null","javaweb后端");
list.add(stu1);
Student stu2 = new Student("柴梧炫","网络","1402","JavaWeb后端");
list.add(stu2);
Student stu3 = new Student("丹文","软件","1401","javaweb后端");
list.add(stu3);
Student stu4 = new Student("樊肥肥","软件工程","null","JavaWeb后端");
list.add(stu4);
Student stu5 = new Student("郭小杰","网络","1401","JavaWeb后端");
list.add(stu5);
Student stu6 = new Student("何小飞","信管","1403","JavaWeb后端");
list.add(stu6);
Student stu7 = new Student("李龙飞","计算机","1402","JavaWeb后端");
list.add(stu7);
Student stu8 = new Student("李自攀","软件","1503","JavaWeb后端");
list.add(stu8);
Student stu9 = new Student("刘琳珂","信管","1403","JavaWeb后端");
list.add(stu9);
Student stu10 = new Student("倪森森","null","null","JavaWeb后端");
list.add(stu10);
Student stu11= new Student("史玉洁","网络工程","1401","javaWeb后端 ");
list.add(stu11);
Student stu12= new Student("王明明","网络工程","1403","javaWeb后端");
list.add(stu12);
Student stu13= new Student("吴同生","计算机 ","null","javaWeb 后端");
list.add(stu13);
Student stu14= new Student("夏方涛","网络","null", "JavaWeb后端");
list.add(stu14);
Student stu15 = new Student("谢永杰","网络工程","1403","JavaWeb后端");
list.add(stu15);
Student stu16= new Student("杨建军","软件","null","JavaWeb后端");
list.add(stu16);
Student stu17= new Student("于涵","信管","null","JavaWeb后端");
list.add(stu17);
Student stu18= new Student("张东升","计算机","1401","JavaWeb后端");
list.add(stu18);
Student stu19= new Student("张豪建","计算机","1401","JavaWeb后端");
list.add(stu19);
Student stu20= new Student("张衡","软件","1503","JavaWeb后端");
list.add(stu20);
Student stu21 = new Student("张森鑫","null","null","JavaWeb后端");
list.add(stu21);
Student stu22 = new Student("赵仕魁","计算机","null","JavaWeb后端");
list.add(stu22);
Student stu23 = new Student("郑震","软件","1403","JavaWeb后端");
list.add(stu23);
Student stu24 = new Student("刘群","null","null","JavaWeb后端");
list.add(stu24);
Student stu25 = new Student("王文东","null","null","JavaWeb后端");
list.add(stu25);
Student stu26 = new Student("孔祥宝","null","null","JavaWeb后端");
list.add(stu26);
}
}
创建所需要的方法,查找和删除方法,再删除后 对集合进行遍历:
package com.cn.list;
import java.util.List;
public class Mthod {
Stu stu=new Stu();
public Student findname(String name){
stu.getlist();
int i;
for(i=0;i<stu.list.size();i++){
if(stu.list.get(i).getStuname().equals(name))//查找与输入名字相符的
{
return stu.list.get(i);
}
}
return null;//没找到,则报错;
}
public List<Student> delete(String name){
stu.getlist();
int i;
for(i=0;i<stu.list.size();i++){
if(stu.list.get(i).getStuname().equals(name))
{
stu.list.remove(i);
return stu.list;
}
}
return null;
}
}
这是我第一个上传的博客……