日期:2014-05-20 浏览次数:20793 次
public class Queue { private static final int DEFAULT = 10; private Object[] queue; /** * 数组的长度 */ private int length = 0; /** * 当前存储了多少数据 */ private int size = 0; public Queue() { queue = new Object[DEFAULT]; length = DEFAULT; } public Queue(int length) { queue = new Object[length]; this.length = length; } public void set(Object obj) { if (size >= length) { Object[] newQueue = new Object[length + DEFAULT]; System.arraycopy(queue, 0, newQueue, 0, length); length = length + DEFAULT; queue = newQueue; } queue[size] = obj; size++; } public Object get() { Object object = queue[0]; size--; for (int i = 0; i < size; i++) { queue[i] = queue[i + 1]; } queue[size] = null; return object; } public int size() { return size; } }
------解决方案--------------------
Stack s = new Stack();
char[] chars = "zhouyongyang".toCharArray();
int length = chars.length;
StringBuffer ss= new StringBuffer();
for(int i = 0; i < length; i++)
{
s.add(chars[i]);
}
for(int i = 0; i < chars.length; i++)
{
ss.append(s.pop());
}
这很快,,,我测试过了,,,当字符串大时,,,要比11楼的快几十部可能会更大