一个处理字符数组的问题
主要问题在于remove和pack怎么写pack里面照我这么写肯定达不到目的的。按说,应该要写成这样才是对的。
int i;
for(i = pos; i < queue.length; i++)
{
queue[i] = queue[i+1];
}
可是这么写编译器却不允许。为什么??
程序代码:
package Lab01; /** * @(#)Queue_skeleton.java * * This is a skeleton for the class Queue. If you build your class from this * skeleton, rename the file "Queue.java". * * A queue for String-objects, implemented with an array, automatically * extended when needed. */ public class Queue { // Members private String[] queue; private int noOfElements = 0; // Capacity = queue.length, no need for an extra member. public Queue(int size) { // Create the String-array queue = new String[5]; } // Accessors public int getNoOfElements() { // ... return noOfElements; } public boolean isEmpty() { // Check, and return, whether the queue is empty. int k = 0; for(int i = 0; i < queue.length; i++) { if(queue[i] != null) k++; } if(k != 0) return false; else return true; } // Mutator. Adds a string to the end of the que. public void add(String s) { // First, check if the array is full. If so call resize(). int k = 0; for(int i = 0; i < queue.length; i++) { if(queue[i] != null) { k++; } } if(k == queue.length) { resize(); } // Second, add s at end of queue and increment noOfElements. queue[noOfElements++] = s; //noOfElements++; } // Mutator. Removes the head of the queue. public String remove() { String tt = new String(); // If the queue is empty, return null. // Else, create a temporary String-reference referencing // the first element int k = 0; for(int i = 0; i < queue.length; i++) { if(queue[i] != null) k++; } if(k == 0) { return null; } else { tt = this.queue[0]; } // Move all elements after the first one step, i.e pack // the array (this will overwrite the first position in the // array). pack(0); // Return the first element via the tempoarary reference. return tt; } public String toString() { // Create and return a String of the form // "[first element, second element,...]". String text = new String(); text = text + "["; for(int i = 0; i < queue.length; i++) { text = text + queue[i]; text = text + ","; } text = text + "]"; return text; } private void resize() { // Create a new, temporary array, twice the size. //int d = queue.length * 2; String[] temp = new String[queue.length]; // Copy all elements from queue to the temporary array. for(int i = 0; i < queue.length; i++) { temp[i] = queue[i]; } queue = temp; // Let the member queue reference the new array // (queue = temp;). NB, the original array will be // automatically "garbage collected". } private void pack(int pos) { // In a loop, copy element at (pos+1) to pos, and so on // up to the last string in the queue. // Decrement noOfElements. int i = 0; for(i = pos; i < queue.length; i++) { queue[pos] = queue[pos+1]; } noOfElements--; } }
程序代码:
package Lab01; /** * A main method, testing the class Queue. */ public class WaitingRoom { public static void main(String[] args) { Queue q = new Queue(3); q.add("Steve"); q.add("Erykah"); q.add("Curtis"); q.add("Aretha"); q.add("Marvin"); System.out.println("The queue: " + q.toString()); String next; next = q.remove(); System.out.println("Next: " + next); next = q.remove(); System.out.println("Next: " + next); System.out.println("The queue: " + q.toString()); q.add("James"); next = q.remove(); System.out.println("Next: " + next); System.out.println("The queue: " + q.toString()); /* //仅仅是为了方便测试,才把最后这部分抹掉 //等到测试全部内容的时候,这部分自然需要加上 while(!q.isEmpty()) { next = q.remove(); System.out.println("Next: " + next); } System.out.println("The queue is empty: " + q.toString()); */ } } 如果remove和pack都写正确的话,打印出来的结果应该是这样的: The queue: [Steve,Erykah,Curtis,Aretha,Marvin,] Next: Steve Next: Erykah The queue: [Curtis,Aretha,Marvin,] Next: Curtis The queue: [Aretha,Marvin,James,] Next: Aretha Next: Marvin Next: James The queue is empty: []
[ 本帖最后由 vdestroyer 于 2009-9-6 20:32 编辑 ]