A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© whocases 黑马帝   /  2012-6-28 20:06  /  5788 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 whocases 于 2012-6-30 19:28 编辑

书上说当调用ByteBuffer.array()的时候可能会抛出UnsupportedOperationException,但当我自己用allocate(int)来创建缓冲区后,再调用array()来获得数组,发觉根本就不会抛出此异常,我想知道是不是API里说错了还是为什么?
import java.nio.IntBuffer;
public class Test3{
public static void main(String[] args){  
  IntBuffer buffer = IntBuffer.allocate(10);   
      for(int i = 0 ; i < 5 ; i ++){     
          buffer.put(i);}        
      int[] array = buffer.array();   
  /**   
   *当是用ByteBuffer.array()方法来获得缓冲区中的数据数组时,
   *此时缓冲区中的数据和对应的数组共享.   
  */   
buffer.flip();   
buffer.put(100);
   for(int i : array){  
    System.out.println(i);
   }  
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马