1概念具集合Java的集合是工具類,可以存儲任意數(shù)量的具有共同屬性的對象。 2體系結(jié)構(gòu)(分為2類一類為Collection一類為Map)
? 1Collection? 1List是元素有序并且可以重復(fù)的序列并且可以重復(fù)的集合 可以精確的控制每個(gè)元素的插入位置,或者刪除某個(gè)位置 主要有兩個(gè)類ArrayList和LinkedList ArrayList底層是由數(shù)組實(shí)現(xiàn)的 動(dòng)態(tài)增長,以滿足應(yīng)用程序的需求 ?? 在列表尾部插入或者刪除數(shù)據(jù)非常有效 更適合查找和更新元素 ArrayList可以為Null; 在ArrayList添加數(shù)據(jù),刪除數(shù)據(jù),和輸出數(shù)據(jù) ![]() package com.jiedada.arraylist;
import java.util.ArrayList;
import java.util.List;
public class ArrayListOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
//創(chuàng)建一個(gè)ArrayList并且添加數(shù)據(jù)
List list=new ArrayList();
list.add("java");
list.add("C ");
list.add("GO");
list.add("C");
list.add("swit");
//輸出結(jié)果
for(int i=0;i<list.size();i )
{
System.out.print(list.get(i) " ");
}
//刪除GO
System.out.println("**************************");
list.remove(2);
for(int i=0;i<list.size();i )
{
System.out.print(list.get(i) " ");
}
}
}View Code?公告通知實(shí)列 1公告添加和顯示 2在指定位置插入公告 3刪除公告 4顯示公告 注意:Data方法是JAVA中自帶的方法,我們使用的時(shí)間函數(shù)在uitl中 在ArrayList中數(shù)組的長度方法為.size,刪除方法為remove;set為修改數(shù)據(jù);set不能更改ArrayList的長度; 創(chuàng)建廣告類 ![]() package com.jiedada.arraylist;
import java.util.Date;
public class Notice {
private int id;
private String name;
private Date date;
private String title;
public Notice(int id, String name, Date date, String title) {
super();
this.id = id;
this.name = name;
this.date = date;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}View Code? 對廣告類進(jìn)行改動(dòng) ![]() package com.jiedada.arraylist;
import java.util.ArrayList;
import java.util.Date;
public class NoticeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//創(chuàng)建Notice類,生成公告
Notice notice1=new Notice(1, "jiedada",new Date(),"歡迎來到杰大大這里");
Notice notice2=new Notice(2, "jiedada",new Date(),"不準(zhǔn)玩網(wǎng)絡(luò)游戲");
Notice notice3=new Notice(3, "jiedada",new Date(),"不能在電腦上亂動(dòng)?xùn)|西哦");
//添加公告
ArrayList noticelist=new ArrayList();
noticelist.add(notice1);
noticelist.add(notice2);
noticelist.add(notice3);
//顯示內(nèi)容
for(int i=0;i<noticelist.size();i )
{
System.out.println(((Notice)(noticelist.get(i))).getTitle());
}
System.out.println("**************************");
//在公告中添加一個(gè)杰是大帥哥
Notice notice4=new Notice(4, "jiedadadexiaomimei",new Date(), "杰是大帥哥");
noticelist.add(notice4);
for(int i=0;i<noticelist.size();i )
{
System.out.println(((Notice)(noticelist.get(i))).getTitle());
}
System.out.println("**************************");
//刪除數(shù)據(jù)
noticelist.remove(3);
for(int i=0;i<noticelist.size();i )
{
System.out.println(((Notice)(noticelist.get(i))).getTitle());
}
System.out.println("**************************");
//修改數(shù)據(jù)
notice2.setTitle("杰大大不在的時(shí)候不準(zhǔn)玩游戲");
noticelist.set(1,notice2);
for(int i=0;i<noticelist.size();i )
{
System.out.println(((Notice)(noticelist.get(i))).getTitle());
}
}
}View Code? ?? 2Queue3Set?是元素?zé)o序并且不重復(fù)的集合。 HashSet是Set的重要實(shí)現(xiàn)類,叫哈希集 HashSet的元素?zé)o序并且不重復(fù) HashSet中只允許一個(gè)null 具有良好的查找和存儲功能 在這個(gè)類中沒有g(shù)et()方法需要使用迭代器 迭代器是一個(gè)接口也是在uitl中的,迭代器每次使用的時(shí)候最好在定義一次; 在迭代器中.next()方法中用用到向下轉(zhuǎn)型,這樣很容易出錯(cuò),我們不知道。next()方法中是不是全部都是這樣的類型的類; 所以我們引入泛型,在定義的時(shí)候這樣定義Set<Cat> set=new HashSet<Cat>(); 有hasNext()方法檢測集合中是否還有元素; next()返回集合中的下一個(gè)元素; 代碼如下 ![]() package com.jiedada.arraylist;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class hashhanshu {
public static void main(String[] args) {
// TODO Auto-generated method stub
//添加顏色
Set set=new HashSet();
set.add("blue");
set.add("red");
set.add("yellow");
set.add("wilte");
set.add("green");
//用迭代器進(jìn)行輸出
Iterator it=set.iterator();
while(it.hasNext())
{
System.out.print(it.next() " ");
}
System.out.println("**************************");
//添加wilte
set.add("wilte");
it=set.iterator();
while(it.hasNext())
{
System.out.print(it.next() " ");
}
System.out.println("**************************");
}
}View Code?在添加相同屬性時(shí),HashSet不會自動(dòng)判斷需要重寫HashCode和equles方法; 什么是哈希表:通過特定的規(guī)則把數(shù)據(jù)分為幾份,在在數(shù)據(jù)中找到我們需要的數(shù)據(jù); 如通過n%3分為3分,0為一份,1為一份,2為一份; Cat的方法 ![]() package com.jiedada.arraylist;
public class Cat {
String name;
int mouth;
String speice;
public Cat(String name, int mouth, String speice) {
super();
this.name = name;
this.mouth = mouth;
this.speice = speice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMouth() {
return mouth;
}
public void setMouth(int mouth) {
this.mouth = mouth;
}
public String getSpeice() {
return speice;
}
public void setSpeice(String speice) {
this.speice = speice;
}
@Override
public String toString() {
return "Cat [name=" name ", mouth=" mouth ", speice=" speice "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result mouth;
result = prime * result ((name == null) ? 0 : name.hashCode());
result = prime * result ((speice == null) ? 0 : speice.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if(this==obj)
{
return false;
}
if(obj.getClass()==Cat.class)
{
Cat cat=(Cat)obj;
return cat.getName().equals(name)&&(cat.getMouth()==mouth)&&cat.getSpeice().equals(speice);
}
return true;
}
}View Codetest的方法 ![]() package com.jiedada.arraylist;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CatTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cat huahua=new Cat("花花",12,"英國貓");
Cat fanfan=new Cat("凡凡",3,"中國田園貓");
Set set=new HashSet();
set.add(huahua);
set.add(fanfan);
Iterator it=set.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("**************");
//添加屬性
Cat cat=new Cat("花花",12,"英國貓");
set.add(cat);
it=set.iterator();
System.out.println("改變后的結(jié)果");
while(it.hasNext())
{
System.out.println(it.next());
}
}
}View Code?上面的所有增,刪,改,查的代碼 ![]() package com.jiedada.arraylist;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CatTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cat huahua=new Cat("花花",12,"英國貓");
Cat fanfan=new Cat("凡凡",3,"中國田園貓");
Set<Cat> set=new HashSet<Cat>();
set.add(huahua);
set.add(fanfan);
Iterator<Cat> it=set.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("**************");
//添加屬性
System.out.println("*****************");
Cat cat=new Cat("花花",12,"英國貓");
set.add(cat);
it=set.iterator();
System.out.println("改變后的結(jié)果");
while(it.hasNext())
{
System.out.println(it.next());
}
Cat huahua2=new Cat("花花2",1,"英國貓");
//通過對想找到元素
System.out.println("*****************");
set.add(huahua2);
if(set.contains(huahua))
{
System.out.println("找到了花花!");
System.out.println(huahua);
}
else
{
System.out.println("沒有找到");
}
//通過名字找到元素
System.out.println("*****************");
Cat cat1=new Cat();
boolean flag=false;
it=set.iterator();
while(it.hasNext())
{
cat1=(Cat)it.next();
if(cat1.getName()=="花花")
{
flag=true;
break;
}
}
if(flag)
{
System.out.println("找到了花花!");
System.out.println(cat1);
}
else
{
System.out.println("沒有找到!");
}
System.out.println("*****************");
//刪除花花的數(shù)據(jù)并且用增強(qiáng)型FOR循環(huán)
/* for(Cat cat2:set)
{
if(cat2.getName().equals("花花"))
{
set.remove(cat2);
break;
}
}*/
//刪除多個(gè)數(shù)據(jù)的方法;
Set<Cat> set1=new HashSet<Cat>();
for(Cat cat2:set)
{
if(cat2.getMouth()<5)
{
set1.add(cat2);
}
}
set.removeAll(set1);
for(Cat cat2:set)
{
System.out.println(cat2);
}
System.out.println("*****************");
//刪除所有數(shù)據(jù)
boolean flag1=set.removeAll(set);
if(set.isEmpty())
{
System.out.println("沒有數(shù)據(jù)了!");
}
else
{
System.out.println("還有數(shù)據(jù)");
}
}
}View Code? Mapmap中的數(shù)據(jù)是以鍵值對(key-value)的形式儲存的 key-value以Entry類型的對象實(shí)例存在 可以通過key值快速查找value 一個(gè)映射不能包含重復(fù)的鍵:一個(gè)key只能對應(yīng)一個(gè)value,但是一個(gè)value可以對應(yīng)多個(gè)key HashMap基于哈希表的Map接口的實(shí)現(xiàn) 允許使用null值和null鍵 key值不允許重復(fù) HashMap中的Entry對象是無序排列的 實(shí)現(xiàn)一個(gè)字典的添加和輸出數(shù)據(jù); 通過對象.put輸入數(shù)據(jù) 再通過把key和values的值通過對象.entrySet()存入Set<Entry<String,String>>的方法 ![]() package com.jiedada.arraylist;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Dictionliry {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> animal=new HashMap<String,String>();
Scanner console=new Scanner(System.in);
int i=0;
//添加key和value的值
while(i<3)
{
System.out.println("請輸入key(單詞)的值");
String key=console.next();
System.out.println("請輸入value(注釋)的值");
String value=console.next();
animal.put(key, value);
i ;
}
//通過迭代器的方法輸出value的值
Iterator<String> it=animal.values().iterator();
System.out.println("迭代器輸出values:");
while(it.hasNext())
{
System.out.print(it.next() " ");
}
System.out.println();
System.out.println("*******************");
//輸出key和values
Set<Entry<String, String>> set=animal.entrySet();
for(Entry<String, String> a:set)
{
System.out.print(a.getKey() "-");
System.out.println(a.getValue());
}
}
}View Code完整的貨物存儲代碼 Goods代碼 ![]() package com.jiedada.arraylist;
public class Goods {
private String id;
private String name;
private int price;
public Goods(String id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Goods [id=" id ", name=" name ", price=" price "]";
}
}View Code? Goodstest ![]() package com.jiedada.arraylist;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class GoodsTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,Goods> goodsMap=new HashMap<String,Goods>();
System.out.println("請輸入三天信息:");
Scanner console=new Scanner(System.in);
//錄入信息
int i=0;
while(i<3)
{
System.out.println("請輸入商品編號:");
String id=console.next();
if(goodsMap.containsKey(id))
{
System.out.println("商品名稱重復(fù),請?jiān)诖溯斎?quot;);
continue;
}
System.out.println("請輸入商品名字:");
String name=console.next();
int price;
try {
System.out.println("請輸入商品價(jià)格:");
price=console.nextInt();
}catch(java.util.InputMismatchException e)
{
System.out.println("價(jià)格為整數(shù)請重新輸入數(shù)字");
console.next();
continue;
}
Goods goods=new Goods(id,name,price);
//把值輸入其中
goodsMap.put(id, goods);
i ;
}
//遍歷找出
Iterator<Goods> it=goodsMap.values().iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
//輸出values和key的數(shù)據(jù)
}
}View Code? 3實(shí)際應(yīng)用1無法預(yù)測存儲數(shù)據(jù)的數(shù)量2同時(shí)存儲具有一對一關(guān)系的數(shù)據(jù)3需要進(jìn)行數(shù)據(jù)的增刪4數(shù)據(jù)重復(fù)問題 |
|
|