public static void main(String[] args) {
LinkedList<String> strings = new LinkedList<>();
strings.push("1");
strings.push("2");
strings.push("3");
System.out.println(strings);
while (strings.size()!=0){
System.out.print(strings.removeLast()+" ");
}
}
public static List test2(int count){
Random random = new Random();
ArrayList<Integer> list = new ArrayList<>();
int t;
while (list.size()!=count){
t = random.nextInt(10);
if(!list.contains(t))
list.add(t);
}
return list;
}
-
【中】程序设计:图书管理器,设计⼀个图书管理器,实现对图书进⾏的存储管 理操作,实现功能
1.添加⼀本图书(书名、作者(姓名,年龄,性别)、售价)
2.删除⼀本图书(通过书名删除)
3.删除所有的指定作者的书(通过作者姓名删除)
4.将所有的图书按照图书售价降序排序。若售价相同,按照作者年龄升序)
图书类
public class Book { private String bookName; private Author author; private Double price; public Book() { } public Book(String bookName, Author author, Double price) { this.bookName = bookName; this.author = author; this.price = price; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public Author getAuthor() { return author; } public void setAuthor(Author author) { this.author = author; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Book{" + "bookName='" + bookName + '\'' + ", author=" + author + ", price=" + price + '}'; } }
作者类
public class Author { private String name; private int age; private String sex; public Author() { } public Author(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Author{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
图书管理器类
public class LibraryManagement { private List<Book> books; public LibraryManagement(List<Book> books) { this.books = books; } public LibraryManagement() { } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public boolean deleteByName(String name){ if(books.contains(name)){ books.remove(name); return true; } return false; } public boolean deleteByAuthorName(String name){ ListIterator<Book> bookListIterator = books.listIterator(); int size = books.size(); while (bookListIterator.hasNext()){ if(bookListIterator.next().getAuthor().getName().equals(name)){ bookListIterator.remove(); } } return size!=books.size(); } public void sortBook(){ Collections.sort(books,(o1,o2)->{ if(o1.getPrice()!=o2.getPrice()) return (int)(o1.getPrice() - o2.getPrice()); else return o1.getAuthor().getName().compareTo(o2.getAuthor().getName()); }); } }
测试类
public class Test { public static void main(String[] args) { LibraryManagement libraryManagement = new LibraryManagement(); ArrayList<Book> books = new ArrayList<>(); books.add(new Book("三国演义",new Author("罗贯中",1000,"男"),399.0)); books.add(new Book("水浒传前",new Author("施耐庵",999,"男"),199.9)); books.add(new Book("水浒传后",new Author("罗贯中",999,"男"),299.9)); books.add(new Book("红楼梦",new Author("曹雪芹",9000,"男"),499.9)); libraryManagement.setBooks(books); System.out.println("排序前"+books); libraryManagement.sortBook(); System.out.println("排序后"+books); libraryManagement.deleteByName("红楼梦"); System.out.println(books); libraryManagement.deleteByAuthorName("罗贯中"); System.out.println(books); } }
-
创建⼀个Map集合,存⼊5个学⽣信息,使⽤学号和学⽣基本信息进⾏对应,学 ⽣基本信息包括姓名,年龄,专业,遍历输出。
public static void test3(){ HashMap<Long, Object[]> map = new HashMap<>(); map.put(2101L,new Object[]{"张三",21,"计算机"}); map.put(2102L,new Object[]{"李四",21,"英语"}); map.put(2103L,new Object[]{"王五",21,"金融"}); for(Long id: map.keySet()){ System.out.println(id+" "+map.get(id)[0]+" "+map.get(id)[1]+" "+map.get(id)[2]); } }
public static void test4(){ HashMap<Integer, HashMap<String, String>> map = new HashMap<>(); HashMap<String, String> stuMap1 = new HashMap<>(); stuMap1.put("101","张三"); stuMap1.put("102","李四"); stuMap1.put("103","王五"); HashMap<String, String> stuMap2 = new HashMap<>(); stuMap2.put("201","张二三"); stuMap2.put("202","李二四"); stuMap2.put("203","王二五"); map.put(2101,stuMap1); map.put(2102,stuMap2); System.out.println(map); }
public static void test5(){ String[] strings = new String[5]; strings[0] = "I hava an apple"; strings[1] = "I hava a apple"; strings[2] = "hello world"; strings[3] = "how are you"; strings[4] = "I hava a dog"; HashMap<String, Integer> map = new HashMap<>(); for (String string : strings) { String[] split = string.split(" "); for (String s : split) { if(map.get(s.toLowerCase())==null){ map.put(s.toLowerCase(),1); }else map.put(s.toLowerCase(),map.get(s.toLowerCase())+1); } } System.out.println(map); }
public static void test6(){ String str = "aa@sohu.com,bb@163.com,cc@sina.com"; String[] split = str.split(","); HashMap<String, String> map = new HashMap<>(); for (String s : split) { String[] split1 = s.split("@"); map.put(split1[0],split1[1]); } System.out.println(map); }
-