乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java集合

       印度阿三17 2019-04-20

      1概念具

      集合Java的集合是工具類,可以存儲任意數(shù)量的具有共同屬性的對象。

      2體系結(jié)構(gòu)(分為2類一類為Collection一類為Map)

      ?

      1Collection

      ?

      1List

      是元素有序并且可以重復(fù)的序列并且可以重復(fù)的集合

      可以精確的控制每個元素的插入位置,或者刪除某個位置

      主要有兩個類ArrayList和LinkedList

      ArrayList

      底層是由數(shù)組實現(xiàn)的

      動態(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)建一個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

      ?公告通知實列

      1公告添加和顯示

      2在指定位置插入公告

      3刪除公告

      4顯示公告

      注意:Data方法是JAVA中自帶的方法,我們使用的時間函數(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)行改動

      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(),"不能在電腦上亂動?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("**************************");
              //在公告中添加一個杰是大帥哥
              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("杰大大不在的時候不準(zhǔn)玩游戲");
              noticelist.set(1,notice2);
              for(int i=0;i<noticelist.size();i  )
              {
                  System.out.println(((Notice)(noticelist.get(i))).getTitle());
              }
          }
      
      }
      View Code

      ?

      ?

      ?

      2Queue

      3Set

      ?是元素?zé)o序并且不重復(fù)的集合。

      HashSet

      是Set的重要實現(xiàn)類,叫哈希集

      HashSet的元素?zé)o序并且不重復(fù)

      HashSet中只允許一個null

      具有良好的查找和存儲功能

      在這個類中沒有g(shù)et()方法需要使用迭代器

      迭代器是一個接口也是在uitl中的,迭代器每次使用的時候最好在定義一次;

      在迭代器中.next()方法中用用到向下轉(zhuǎn)型,這樣很容易出錯,我們不知道。next()方法中是不是全部都是這樣的類型的類;

      所以我們引入泛型,在定義的時候這樣定義Set<Cat> set=new HashSet<Cat>();

      有hasNext()方法檢測集合中是否還有元素;

      next()返回集合中的下一個元素;

      代碼如下

      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

      ?在添加相同屬性時,HashSet不會自動判斷需要重寫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 Code

      test的方法

      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ù)并且用增強型FOR循環(huán)
             /* for(Cat cat2:set)
              {
                  if(cat2.getName().equals("花花"))
                  {
                      set.remove(cat2);
                  break;
                  }
              }*/
              //刪除多個數(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

      ?

      Map

      map中的數(shù)據(jù)是以鍵值對(key-value)的形式儲存的

      key-value以Entry類型的對象實例存在

      可以通過key值快速查找value

      一個映射不能包含重復(fù)的鍵:一個key只能對應(yīng)一個value,但是一個value可以對應(yīng)多個key

      HashMap

      基于哈希表的Map接口的實現(xiàn)

      允許使用null值和null鍵

      key值不允許重復(fù)

      HashMap中的Entry對象是無序排列的

      實現(xiàn)一個字典的添加和輸出數(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ù),請在此輸入");
                      continue;
                  }
                  System.out.println("請輸入商品名字:");
                  String name=console.next();
                  int price;
                  try {
                  System.out.println("請輸入商品價格:");
                  price=console.nextInt();
                  }catch(java.util.InputMismatchException e)
                  {
                      System.out.println("價格為整數(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實際應(yīng)用

      1無法預(yù)測存儲數(shù)據(jù)的數(shù)量

      2同時存儲具有一對一關(guān)系的數(shù)據(jù)

      3需要進(jìn)行數(shù)據(jù)的增刪

      4數(shù)據(jù)重復(fù)問題

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多