• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

java 随机队列实现的完整方法

JAVA相关 水墨上仙 2239次浏览

java 随机队列实现的完整方法

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Random;

public class RandomQueue implements Queue {
	static int Squads = 32;
	static int wid = 8;
	@SuppressWarnings("unchecked")
	LinkedList[][] bulk = new LinkedList[wid][wid];
	Random r;
	int count = 0;

	@Override
	public int size() {
		return count;
	}

	public RandomQueue() {
		r = new Random(System.currentTimeMillis());
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j] = new LinkedList();
			}
		}
	}

	@Override
	public boolean isEmpty() {
		return count > 0 ? false : true;
	}

	@Override
	public boolean contains(Object o) {
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				if (bulk[i][j].contains(o)) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public Iterator iterator() {
		ArrayList list = new ArrayList();
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].toArray();
				list.addAll(bulk[i][j]);
			}
		}
		return list.iterator();
	}

	@Override
	public Object[] toArray() {
		ArrayList list = new ArrayList();
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].toArray();
				list.addAll(bulk[i][j]);
			}
		}
		return list.toArray();
	}

	@Override
	public  T[] toArray(T[] a) {
		ArrayList list = new ArrayList();
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].toArray();
				list.addAll(bulk[i][j]);
			}
		}
		return list.toArray(a);
	}

	@Override
	public boolean remove(Object o) {
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				if (bulk[i][j].contains(o)) {
					return bulk[i][j].remove(o);
				}
			}
		}
		return false;
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		ArrayList list = new ArrayList();
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].toArray();
				list.addAll(bulk[i][j]);
			}
		}
		return list.containsAll(c);
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		int i = r.nextInt(wid);
		int j = r.nextInt(wid);
		return bulk[i][j].addAll(c);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].removeAll(c);
			}
		}
		return true;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].retainAll(c);
			}
		}
		return true;
	}

	@Override
	public void clear() {
		for (int i = 0; i < wid; ++i) {
			for (int j = 0; j < wid; ++j) {
				bulk[i][j].clear();
			}
		}
	}

	@Override
	public boolean add(E e) {
		int i = r.nextInt(wid);
		int j = r.nextInt(wid);
		return bulk[i][j].add(e);
	}

	@Override
	public boolean offer(E e) {
		return add(e);
	}

	@Override
	public E remove() {
		int i = r.nextInt(wid);
		int j = r.nextInt(wid);
		for (int k = i; k < wid; ++k) {
			for (int l = j; l < wid; ++l) {
				if (bulk[k][l].isEmpty()) {
					continue;
				}
				return bulk[k][l].remove();
			}
		}
		for (int k = 0; k < i; ++k) {
			for (int l = 0; l < j; ++l) {
				if (bulk[k][l].isEmpty()) {
					continue;
				}
				return bulk[k][l].remove();
			}
		}
		throw new NoSuchElementException();
	}

	@Override
	public E poll() {
		int i = r.nextInt(wid);
		int j = r.nextInt(wid);
		try {
			for (int k = i; k < wid; ++k) {
				for (int l = j; l < wid; ++l) {
					if (bulk[k][l].isEmpty()) {
						continue;
					}
					return bulk[k][l].remove();
				}
			}
			for (int k = 0; k < i; ++k) {
				for (int l = 0; l < j; ++l) {
					if (bulk[k][l].isEmpty()) {
						continue;
					}
					return bulk[k][l].remove();
				}
			}
		} catch (NoSuchElementException e) {
			return null;
		}
		return null;
	}

	@Override
	public E element() {
		int i = r.nextInt(wid);
		int j = r.nextInt(wid);
		for (int k = i; k < wid; ++k) {
			for (int l = j; l < wid; ++l) {
				if (bulk[k][l].isEmpty()) {
					continue;
				}
				return bulk[k][l].getFirst();
			}
		}
		for (int k = 0; k < i; ++k) {
			for (int l = 0; l < j; ++l) {
				if (bulk[k][l].isEmpty()) {
					continue;
				}
				return bulk[k][l].getFirst();
			}
		}
		throw new NoSuchElementException();
	}

	@Override
	public E peek() {
		int i = r.nextInt(wid);
		int j = r.nextInt(wid);
		try {
			for (int k = i; k < wid; ++k) {
				for (int l = j; l < wid; ++l) {
					if (bulk[k][l].isEmpty()) {
						continue;
					}
					return bulk[k][l].getFirst();
				}
			}
			for (int k = 0; k < i; ++k) {
				for (int l = 0; l < j; ++l) {
					if (bulk[k][l].isEmpty()) {
						continue;
					}
					return bulk[k][l].getFirst();
				}
			}
		} catch (NoSuchElementException e) {
			return null;
		}
		return null;
	}

}

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明java 随机队列实现的完整方法
喜欢 (0)
加载中……