- 请使用LinkedList来模拟一个队列(先进先出的特性):
- 拥有放入对象的方法void put(Object o)
- 取出对象的方法Object get()
- 判断队列当中是否为空的方法boolean isEmpty();并且,编写测试代码,验证你的队列是否正确。
public class Linkeds { List l; Linkeds(){ l=new LinkedList(); } public void put(Object o){ l.add(o); } public Object get(){ Object o= l.get(0); l.remove(0); return o; } public boolean isEmpty(){ if(l.isEmpty()){ return true; } return false; } public static void main(String[] args){ //定义一个空队列 Linkeds l = new Linkeds(); //往队列中放入对象 l.put("Tom1"); l.put("John2"); l.put("Mary3"); //如果队列不为空,依次输出队列中的元素 while(!l.isEmpty()){ System.out.println(l.get()); } } }
- 假设顺序列表ArrayList中存储的元素是整型数字1~5,遍历每个元素,将每个元素顺序输出。
public class ListIteratorExercise { public static void main(String args[]) { List ll = new ArrayList(); // 为数组赋值 for (int i = 1; i < 6; i++) { ll.add(new Integer(i)); } // 遍历输出每一个元素 ListIterator lit = ll.listIterator(); while (lit.hasNext()) { System.out.print(lit.next() + " "); } System.out.println(); // 逆序输出每个元素的值 while (lit.hasPrevious()) { System.out.print(lit.previous() + " "); } } }
- 在一个列表中存储以下元素:apple,grape,banana,pear
1)返回集合中的最大的和最小的元素
2)将集合进行排序,并将排序后的结果打印在控制台上
public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<String> list =new ArrayList<String>(); list.add("apple"); list.add("grape"); list.add("banana"); list.add("pear"); Object[] s= list.toArray(); System.out.println("最小数是"+s[0]); System.out.println("最大数是"+s[s.length-1]); for(Object o:s){ System.out.println(o); } } }
- 编写一个程序,创建一个 HashMap对象,用于存储银行储户的信息(其中储户的主要信息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。
class Account{ private String id; private String name; private double balance; public Account(){ } public Account(String id,String name,double bal) { this.id = id; this.name = name; this.balance = bal; } /** * 取得帐户余额 * @return */ public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class HashMapExample { /** * 取得某个帐户的余额 * @param accounts * @param id * @return */ public double getBalance(Map<String,Account> accounts,String id){ double result = 0; System.out.print("帐户:"+id); return accounts.get(id).getBalance(); } public static void main(String args[]) { Map<String,Account> h = new HashMap<String,Account>(); Account a1 = new Account("1001","zhangsan",12345.99); Account a2 = new Account("1002","tom",8888.50); Account a3 = new Account("1003","mary",6666); h.put("1001", a1); h.put("1002", a2); h.put("1003", a3); HashMapExample hme = new HashMapExample(); System.out.println(" 余额:" + hme.getBalance(h, "1002")); //遍历map Set keySet= h.keySet(); Iterator it = keySet.iterator(); double sum = 0; while(it.hasNext()){ Object key = it.next(); Account a = h.get(key); System.out.println("账户:"+a.getId()+"余额:"+a.getBalance()); //计算所有帐户的余额之和 sum += a.getBalance(); } System.out.println("所有账户余额:"+sum); } }
- 从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。
public class Test { public static void main(String[] args) { List<String> list = new ArrayList<String>(); Scanner in = new Scanner(System.in); String s = in.nextLine(); String[] a = s.split(" "); for(int i=0;i < a.length;i++){ for(int j =i+1;j<a.length;j++){ if(a[i].compareToIgnoreCase(a[j])>0){ s = a[i]; a[i]= a[j]; a[j] = s; } } } for(String o:a){ list.add(o); System.out.println(o); } } }