| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 663 人关注过本帖
标题:一个处理字符数组的问题
只看楼主 加入收藏
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
结帖率:96.43%
收藏
已结贴  问题点数:20 回复次数:3 
一个处理字符数组的问题
主要问题在于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 编辑 ]
搜索更多相关主题的帖子: 字符 
2009-09-06 20:29
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:10 
是编译不能通过呢?还是报错?

C#超级群 74862681,欢迎大家的到来!
2009-09-06 21:12
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
收藏
得分:0 
回复 2楼 gameohyes
编译不能通过
2009-09-07 03:42
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:10 
数组越界啦!

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-09-07 10:33
快速回复:一个处理字符数组的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065390 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved