一条晕的语句

package com.corejava.collec;

import java.util.AbstractQueue;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;

/**
 * @author teddy.lu 06-07-18
 */
public class CircularArrayQueueTest {

 /**
  * @param args
  */
 public static void main(String[] args) {

  Queue<String> q = new CircularArrayQueue<String>(10);
  q.add("Amy");
  q.add("Bob");
  q.add("Carl");
  q.add("Deedee");
  q.add("Emile");

  q.add("Fifi");

  for (Iterator iter = q.iterator(); iter.hasNext();) {
   String element = (String) iter.next();
   System.out.println(element);
  }
  System.out.println("size :" + q.size());

 }

}

class CircularArrayQueue<E> extends AbstractQueue<E> {

 private E[] elements;

 private int head;

 private int tail;

 private int count;

 private int modcount;

 /**
  * constructs an empty queue
  *
  * @param capacity
  */
 public CircularArrayQueue(int capacity) {
  super();
  elements = (E[]) new Object[capacity];
  this.modcount = 0;
  this.count = 0;
  this.head = 0;
  this.tail = 0;
 }

 @Override
 public Iterator<E> iterator() {
  return new QueueIterator();
 }

 @Override
 public int size() {

  return count;
 }

 public boolean offer(E o) {
  assert elements != null;

  // there are still remain space
  if (count < elements.length) {

   elements[tail] = o;
   tail = (tail++) % elements.length;
   count++;
   modcount++;
   return true;
  } else {
   return false;

  }

 }

 public E peek() {
  if (count == 0) {
   return null;
  } else
   return elements[head];
 }

 public E poll() {
  // first judge the elements if empty

  if (count == 0) {
   return null;
  } else {

   E e = elements[head];
   head = (head + 1) % elements.length;
   count--;

   modcount++;
   return e;
  }

 }

 public CircularArrayQueue() {
  super();
 }

 private class QueueIterator implements Iterator<E> {

  private int offset;

  private int modcountAtConstruct;

  public  QueueIterator() {
          System.out.println("modcount is: "+modcount);
   modcountAtConstruct = modcount;
  }

  public boolean hasNext() {

   if (modcount != modcountAtConstruct)
    throw new ConcurrentModificationException();
   return offset < elements.length;
  }

  public E next() {
   if (!hasNext())
    throw new NoSuchElementException();
   E r = elements[(head + offset) % elements.length];
   offset++;
   return r;

  }

  public void remove() {
   throw new UnsupportedOperationException();

  }

 }
}

今天在看core java 7th中关于collection中的内容,在动手运行上面的程序时,发现只保存了最后一个String,结果审阅代码,发现问题竟然出现在   tail = (tail++) % elements.length; 在此记下,以免以后出现同样的错误。 认为这语句其实可以分解成3句来执行(1) tail%elements.length 得 0,(2) tail= tail+1 得 1,(3)再将1的结果赋给tail得0
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值