DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6

Thread: Queue Class

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    12

    Queue Class

    Hi,

    I would like to develop a StringQueue class to hold a queue of String objects.
    I am not exactly sure how I would go about doing this.

    I have created a StringList class which looks something like this:

    Code:
    public class StringList
    {
        // attributes
        private String[] list;
        private int total;
        
        // methods
        public StringList (int sizeIn)
        {  
            list = new String [sizeIn];
            total = 0;
        }
    
    
        public boolean add(String s)
        {   
            if(!isFull())
            {
                list[total] = s;
                total++;
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public boolean isEmpty(
        {
            if(total==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public boolean isFull()
        {
            if(total==list.length)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public String getItem (int i)
        {
            return list [i-1];
        }
        
        public int getTotal()
        {
            return total;
        }
        
        public boolean remove (int numberIn)
        {
            // to be completed
        }
    }
    Any suggestion would be appreciated

    Thanks
    Last edited by Inzaghi; 03-15-2005 at 06:17 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Code:
    public class StringList
    {
        // attributes
        private String[] list;
        private int total;
        
        // methods
        public StringList (int sizeIn)
        {  
            list = new String [sizeIn];
         }
    
    
        public boolean add(String s)
        {   
            if(!isFull())
            {
                list[total++] = s;
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public boolean isEmpty(
        {
           return total==0;
         }
        
        public boolean isFull()
        {
            return total==list.length;
        }
        // this is very 'off-by-one'-error prone, you should go for
        // zero-based indexing.   
        public String getItem (int i)
        {
            return list [i-1];
        }
        
        public int getTotal()
        {
            return total;
        }
        
        public boolean remove (int numberIn)
        {
            // to be completed
        }
    }
    eschew obfuscation

  3. #3
    Join Date
    Dec 2004
    Posts
    12
    Thanks for your improvements i will do that.

    What would i have to change in the code to create a StringQueue class?

    Thanks

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    A queue would need code for shifting all its elements to the next 'slot'. The best
    would then be a linked list consisting of elements like:

    Code:
    public class ListElement {
      public ListElement previous;
      public Object value;
      public ListElement next;
    }
    eschew obfuscation

  5. #5
    Join Date
    Dec 2004
    Posts
    12

    StringQueue

    thanks
    Last edited by Inzaghi; 03-15-2005 at 07:29 AM.

  6. #6
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    java.util.LinkedList<String>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links