标签:bic each 最好 boolean sort opera 欢迎来到 advance import
tangVoice - 唐城好声音
实现选手的增删改查,非常基础的数组操作,不过我了解了arraylist决定用arraylist做
对于一个选手来说肯定是需要用到对象,且将属性封装到对象中合理
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class Tangvoice{ public static void main( String[] args ){ System.out.println("----------欢迎来到唐城好声音----------"); System.out.println("1.显示所有学员信息"); System.out.println("2.根据年龄从小到大排序"); System.out.println("3.随即观看学员技能"); System.out.println("4.删除学员"); System.out.println("5.添加学员"); System.out.println("0.退出"); System.out.println("-----------------------------------"); Scanner input = new Scanner(System.in); ArrayList<Singer> list = new ArrayList<Singer>(); Singer Ji=new Singer("Jikejunyi",23,"wangfeng","pop","pop song singer"); Singer Zhang=new Singer("Zhangbichen",22,"naying","classic","classic song singer"); Singer Liu=new Singer("Liuanqi",28,"jielun","rock","rock song singer"); Singer Liang=new Singer("Liangbo",21,"naying","classic","classic song singer"); Singer Mao=new Singer("Maobiyi",26,"halin","classic","classic song singer"); list.add(Ji); list.add(Zhang); list.add(Liu); list.add(Liang); list.add(Mao); boolean finish = true; while(finish){ System.out.println(); System.out.print("Choose operation: "); switch(input.nextInt()){ case 1:list.forEach(item->item.display()); break; case 2:ArrayList<Singer> temp = new ArrayList<Singer>(); temp.addAll(list); Collections.sort(temp,(a,b)->{ if(a.getAge()>b.getAge()) return 1; else return -1; }); temp.stream().filter(item->item!=null).forEach(item->item.display()); break; case 3: double n = Math.random()*list.size(); list.get((int)n).display(); break; case 4: list.forEach(item->item.display()); System.out.println("delete which singer: "); int index = input.nextInt(); list.remove(index); System.out.println("delete index=" +index+" singer"); break; case 5: System.out.println("enter Singer info:"); System.out.print("Type: "); String type1 = input.next(); System.out.print("Name: "); String name1 = input.next(); System.out.print("Age: "); int age1 = input.nextInt(); System.out.print("Team: "); String team1 = input.next(); Singer newSinger = new Singer(name1,age1,team1,type1,null); list.add(newSinger); System.out.println("add successfully"); break; case 0:finish=false; System.out.println("System quit"); } } } } class Singer{ private String name; private int age; private String team; private String type; private String skill; Singer(String _name, int _age, String _team, String _type, String _skill){ this.name = _name; this.age = _age; this.team = _team; this.type = _type; this.skill = _skill; } public int getAge(){ return this.age; } public String getName(){ return this.name; } public void display(){ System.out.println("Singer type: "+type+", "+"name: "+name+", "+"age: "+age+", "+"team: "+team); } }
总结一下问题:
第一个是UI的问题,每次都要从console看,java的清屏不像c++那么容易(可能是我还没有很了解),最好写swing出来个UI
第二个是ArrayList的复制问题,排序要求不能对原数组操作,换ArrayList的话只能重新建个新ArrayList,再用addAll()方法把旧list加进去,这样是最快的方法么?
第三个是lambda表达式的问题,()->{}这类箭头函数中是不能有赋值操作的,很多功能是不支持的,会提示必须是static或final
第四个是注意泛型的问题,如果在ArrayList中不标注泛型,编译时会出现不安全数据类型的错误,养成好习惯。
preliminary->advanced exam coding part
标签:bic each 最好 boolean sort opera 欢迎来到 advance import
原文地址:https://www.cnblogs.com/exigeslover/p/11931337.html