1、请使用LinkedList来模拟一个队列(先进先出的特性):
1.1 拥有放入对象的方法void put(Object o)
1.2 取出对象的方法Object get()
1.3 判断队列当中是否为空的方法boolean isEmpty();并且,编写测试代码,验证你的队列是否正确。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class QueueDemo {
LinkedList data = new LinkedList<>();
public boolean isEmpty(QueueDemo queueDemo) { return queueDemo==null?true:false; }
public Object get() { Object o = data.getFirst(); data.removeFirst(); return o; }
public void put(Object o) { data.add(o); }
@Override public String toString() { return "QueueDemo{" + "data=" + data + '}'; } }
|
主程序:
1 2 3 4 5 6 7 8 9 10 11 12
| public class TestDemo { public static void main(String[] args) { QueueDemo queue = new QueueDemo(); queue.put(11); queue.put(22); queue.put(33); queue.put(44); System.out.println(queue); System.out.println(queue.get()); System.out.println(queue.isEmpty(queue)); } }
|
测试结果:
1 2 3
| QueueDemo{data=[11, 22, 33, 44]} 11 false
|
3、在一个列表中存储以下元素:apple,grape,banana,pear
3.1、 返回集合中的最大的和最小的元素
3.2 将集合进行排序,并将排序后的结果打印在控制台上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class TestDemo { public static void main(String[] args) {
List<String> data = new ArrayList<String>(); data.add("apple"); data.add("grape"); data.add("banana"); data.add("pear");
System.out.println("最大元素="+Collections.max(data)); System.out.println("最小元素="+Collections.min(data)); Collections.sort(data, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); System.out.println(data); } }
|
输出:
1 2 3
| 最大元素=pear 最小元素=apple [apple, banana, grape, pear]
|
4、编写一个程序,创建一个 HashMap对象,用于存储银行储户的信息(其中储户的主要信息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class Customer { private int id; private String name; private int money;
public Customer(int id, String name, int money) { this.id = id; this.name = name; this.money = money; }
public int getId() { return id; }
public String getName() { return name; }
public int getMoney() { return money; }
@Override public String toString() { return "Customer{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Customer customer = (Customer) o; if (money!=customer.money) return false; return true; }
@Override public int hashCode() { return Objects.hash(id, name, money); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class TestDemo { public static void main(String[] args) { Customer one = new Customer(1,"zhangsan", 100); Customer two = new Customer(2,"lisi", 124); Customer three = new Customer(3,"wangwu", 23);
HashMap<String, Customer> otherData = new HashMap<>(); otherData.put("zhangsan", one); otherData.put("lisi", two); otherData.put("wangwu", three); System.out.println(otherData.get("lisi").getMoney()); } }
|
测试结果:
5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class TestDemo { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); String str = scanner.nextLine(); String [] st=str.split(" "); List<String> list= Arrays.asList(st);
Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } }); System.out.print(list); } }
|
测试结果:
1 2
| 输入: p a h g b v c 输出: [a, b, c, g, h, p, v]
|
6、[ 单选题 ]
Java 中的集合类包括 ArrayList、LinkedList、HashMap 等类,下列关于集合类描述错误的是( C )
A: ArrayList 和 LinkedList 均实现了 List 接口
B ArrayList 的访问速度比 LinkedList 快
C 添加和删除元素时,ArrayList 的表现更佳
D HashMap 实现 Map 接口,它允许任何类型的键和值对象,并允许将 null 用作键或值
7.简答题:
简单叙述一下 Arraylist扩容 与 hashmap 扩容机制 ?
ArrayList是将原数组copy到另一个新扩容的数组里,hashmap是通过单向链表进行扩容的。