笔记22 容器

来源:互联网 发布:北京软件退税政策 编辑:程序博客网 时间:2024/06/04 19:56
package test0915.collectiontest;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** *  容器的概念: 存储多个对象的对象。 * 1存储的元素只能是引用类型的对象; * 2可以随意的添加和删除元素,容器的容量是无限的; *  *  一张图 *   * CollectionMap * /\ | * SetListSortedMap * | * SortedSet *  *  * 三个知识点 * 泛型 * 迭代器: 先判断后获取; hasNext(); nest(); * 比较器: Comparable Comparator 排序; *  *  * 六个接口 *  * Collection * Set * List * Map * Iterator * Comparable *  *  * 九个常用类 *  * ArrayList * HashMap * LinkedList * Properties * HashSet * Collections * Hashtable * TreeSet * Voctor,Stack ... *  *  * 一、Collection * 1、添加: add(E e) 2、删除: remove(Object o)  clear()  3、修改:4、查询: size()  contains(Object o) 5、遍历1)、foreach2)、iterator() 二、List  新增大量关于索引的方法1、添加: add(int index, E element)   add(E e)  2、删除: remove(int index)  remove(Object o)  clear()  3、修改: set(int index, E element) 4、查询: get(int index)  size()  contains(Object o)   indexOf(Object o) 5、遍历1)、foreach2)、iterator() 3)、for(int i=0;i<容器.size();i++){容器.get(i)}三、Set:去重 ,与Collection同理四、Map :HashMap   1、增加:put(K key, V value)2、修改:put(K key, V value)3、删除: remove(Object key)  clear() 4、查看: get(Object key)  size()  containsKey(Object key)  containsValue(Object value) isEmpty() 5、遍历1)、条目集合 entrySet() 2)、键集合 keySet()3)、值的容器 values() * @author Administrator */public class CollectionTest {public static void main(String[] args) {Collection<String> co = new ArrayList<String>();// 添加元素co.add("大毛");co.add("二毛");co.add("小白");co.add("红豆");//遍历 使用迭代器System.out.println("===========迭代器==========");Iterator<String> it = co.iterator();while(it.hasNext() ){System.out.println(it.next() );}System.out.println("=========foreach==========");for(String tem : co){System.out.println(tem);}//其他方法boolean flag = co.contains("小白");//查找System.out.println(flag);co.remove("红豆");//删除System.out.println(co.size() );//查看元素个数co.clear();//清空System.out.println(co.size() );}}


 

package test0915;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** *  * 模拟购物车 ,挑选物品,结账 *  *  * 1、添加: add(E e) 2、删除: remove(Object o)  clear()  3、修改:4、查询: size()  contains(Object o) 5、遍历1)、foreach2)、iterator()  *  * @author Administrator */public class GoodsTest {public static void main(String[] args) {Collection <Goods> col = new ArrayList<Goods>();//挑选商品col.add(new Goods("苹果", 10, 20) );col.add(new Goods("香蕉", 15, 20) );col.add(new Goods("梨", 8, 20) );//结账 == 遍历System.out.println("==============商品列表==============");double total = 0. ;//定义变量 ----- 总金额Iterator<Goods> it = col.iterator();while(it.hasNext() ){Goods temp = it.next();System.out.println(temp.getName()+"-->"+temp.getN()+"-->"+temp.getPrice());total +=temp.getN()*temp.getPrice();}System.out.println("应付:"+total);}}/** * 创建一个物品类根据需求。 */class Goods {private String name;private double price;private int n;public Goods() {//alt+/}//alt+shift+s --> opublic Goods(String name, double price, int n) {super();this.name = name;this.price = price;this.n = n;}//alt+shift+s -->r -->tab+回车 --->shift+tab -->回车public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getN() {return n;}public void setN(int n) {this.n = n;}//alt+shift+s -->h@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + n;result = prime * result + ((name == null) ? 0 : name.hashCode());long temp;temp = Double.doubleToLongBits(price);result = prime * result + (int) (temp ^ (temp >>> 32));return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;final Goods other = (Goods) obj;if (n != other.n)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))return false;return true;}}


 

 

0 0