码迷,mamicode.com
首页 > 其他好文 > 详细

交换相邻字符(CharBuffer)

时间:2016-08-22 19:55:44      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

摘自thinking in java:

public class UsingBuffers {
private static void symmetricScramble(CharBuffer buffer) {
while (buffer.hasRemaining()) {
buffer.mark();
char c1 = buffer.get();
if(buffer.hasRemaining()) {
char c2 = buffer.get();
buffer.reset();
buffer.put(c2).put(c1);
}else {
buffer.reset();
buffer.put(c1);
}
}
}
public static void main(String[] args) {
char[] data = "UsingBuffers7".toCharArray();
ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
CharBuffer cb = bb.asCharBuffer();
cb.put(data);
System.out.println(cb.rewind());
symmetricScramble(cb);
System.out.println(cb.rewind());
}

技术分享

 

交换相邻字符(CharBuffer)

标签:

原文地址:http://www.cnblogs.com/mucheng/p/5796718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!