日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

      歡迎訪問 生活随笔!

      生活随笔

      當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

      生活经验

      java bag集合_集合基于数组的实现:ArrayBag.java

      發布時間:2023/11/27 生活经验 34 豆豆
      生活随笔 收集整理的這篇文章主要介紹了 java bag集合_集合基于数组的实现:ArrayBag.java 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

      /**

      * @Author 陳偉兵

      * @MSN:cwbnig1982@hotmail.com

      * @E-mail:chenweibing1982@sohu.com

      * @CreateTime 2004-11-30

      * @Version:1.0

      */

      package com.cwbnig.util;

      import Java.util.Random;

      import java.util.Iterator;

      public class ArrayBag implements BagADT

      {

      private static Random rand=new Random;

      private final int DEFAULT_CAPACITY =100;

      private final int NOT_FOUND=-1;

      private int count;

      private Object contents;

      /**

      * Create an empty bag using the default capacity

      */

      public ArrayBag

      {

      count=0;

      contents=new Object[DEFAULT_CAPACITY];

      }

      /**

      * Create an empty bag using the specified capacity

      */

      public ArrayBag(int initialCapacity)

      {

      count=0;

      contents=new Object[initialCapacity];

      }

      /**

      * Returns the number of elements currentl in this bag

      */

      public int size

      {

      return count;

      }

      /**

      * Returns true if this bag is empty and false otherwise

      */

      public boolean isEmpty

      {

      return(count==0);

      }

      /**

      * Adds the speacified element to the bag,expanding the capacity of the array if necessary.

      */

      public void add(Object element)

      {

      if(size==contents.length)

      {

      expandCapacity;

      }

      contents[count]=element;

      count++;

      }

      /**

      * Creates a new array to store the contents of the bag with twice the capcity of the old one.

      */

      public void expandCapacity

      {

      Object larger=new Object[contents.length*2];

      for(int index=0;indexbr /> {

      larger[index]=contents[index];

      }

      contents=larger;

      }

      /**

      * Adds the contents of the parameter to this bag.

      */

      public void addAll(BagADT bag)

      {

      Iterator scan=bag.iterator;

      while(scan.hasNext)

      {

      add(scan.next);

      }

      }

      /**

      * Remove a random element from the bag and returns it.Throws and EmptyBagException if the bag is empty

      */

      public Object removeRandomthrows EmptyBagException

      {

      if(isEmpty)

      {

      throw new EmptyBagException;

      }

      int choice=rand.nextInt(count);

      Object result=contents[choice];

      contents[choice]=contents[count-1];

      contents[count-1]=null;

      count--;

      return result;

      }

      /**

      * Removes one occurance of the specified element from the bag and returns it.

      * Throws and EmptyBagException if the bag is empty and a NoSuchElementException if the target is not in the bag.

      */

      public Object remove(Object target)throws EmptyBagException,NoSuchElementException

      {

      int search=NOT_FOUND;

      if(isEmpty)

      {

      throw new EmptyBagException;

      }

      for(int index=0;index {

      search=index;

      }

      if(search==NOT_FOUND)

      {

      throw new NoSuchElementException;

      }

      Object result=contents[count-1];

      contents[search]=contents[count-1];

      contents[count-1]=null;

      return result;

      }

      /**

      * Returns a new bag that is the union of this bag and the parameter.

      */

      public BagADT union(BagADT bag)

      {

      ArrayBag both=new ArrayBag;

      for(int index=0;indexbr /> {

      both.add(contents[index]);

      }

      Iterator scan=bag.iterator;

      while(scan.hasNext)

      {

      both.add(scan.next);

      }

      return both;

      }

      /**

      * Returns true if this bag contains the specified target element.

      */

      public boolean contains(Object target)

      {

      int search=NOT_FOUND;

      for(int index=0;index {

      if(contents[index].equals(target))

      {

      search=index;

      }

      }

      return(search!=NOT_FOUND);

      }

      /**

      * Returns true if this bag contains exactly the same elements as the parameter.

      */

      public boolean equals(BagADT bag)

      {

      boolean result=false;

      ArrayBag temp1=new ArrayBag;

      ArrayBag temp2=new ArrayBag;

      Object obj;

      if(size==bag.size)

      {

      temp1.addAll(this);

      temp2.addAll(bag);

      Iterator scan=bag.iterator;

      while(scan.hasNext)

      {

      obj=scan.next;

      if(temp1.contains(obj))

      {

      try

      {

      temp1.remove(obj);

      temp2.remove(obj);

      }

      catch(EmptyBagException emptyE)

      {

      System.out.println("The current Bag is Empty!");

      }

      catch(NoSuchElementException nosuchE)

      {

      System.out.println("The current Bag has no such element!");

      }

      }

      }

      result=(temp1.isEmpty&&temp2.isEmpty);

      }

      return result;

      }

      /**

      * Returns an iterator for the elements currently in this bag

      */

      public Iterator iterator

      {

      return new ArrayIterator(contents,count);

      }

      /**

      * Returns a string representation of this bag.

      */

      public String toString

      {

      String result="";

      for(int index=0;indexbr /> {

      result=result+contents[index].toString+"n";

      }

      return result;

      }

      }

      查看評論

      總結

      以上是生活随笔為你收集整理的java bag集合_集合基于数组的实现:ArrayBag.java的全部內容,希望文章能夠幫你解決所遇到的問題。

      如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。