Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: i need to add an item to my array in ascending order by using the compareTo method...

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i need to add an item to my array in ascending order by using the compareTo method...

    something is wrong with my boolean add method... something is not working good with my loops. can anyone help me please.
    this is the code I have:

    public boolean add(AnyType x) 
        {  
            if(theSize == items.length)
            {
                grow();
            }
     
            if(theSize == 0)
            {
                items[0] = x;
            }
     
            else if(contains(x) == false)
            {   
     
     
                for(int i = 0; i < theSize; i++)
                {                  
                    if(x.compareTo(items[i])>0)
                    {
     
                        int k = i+1;
     
                        while(k>0 && items[k-1].compareTo(x)<0)
                        {
     
                            items[k+2] = items[k+1];
                            items[k+1] = items[k];
     
                            k--;
                        }
                        items[k+1]=x;
                        break;
     
                    }
                    else if(x.compareTo(items[i])<0)
                    { 
     
                        AnyType swap = items[i];
                        int k=i+1;
     
                        while(k>0 && items[k-1].compareTo(x)>0)
                        {
                            items[k+1] = items[k];
                            k=k-1;
                        }
                        items[k+1]=swap;
                        items[k] = x;
                        break;
     
                    }        
            }
     
            }  
     
            theSize++;
            return false;        
        }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: i need to add an item to my array in ascending order by using the compareTo metho

    Quote Originally Posted by twi View Post
    something is not working good
    Please provide more information. What does "not working" mean? Do you get errors? Then copy and paste the EXACT error message. Do you get incorrect output? Then post your input, expected output and actual output.

    One comment, why does the method always return false? I imagine it shoud return true if the new object was inserted successfully and false otherwise.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i need to add an item to my array in ascending order by using the compareTo metho

    every time i add an item if it contains in the array then boolean add is false and nothing happens like in the output number 2... every time i add a number it enters the item in the position it needs to be in in ascending order ... everything works out fine but when i insert 6 it wont shift all the way to the end... I dont know what is wrong with my code...
    input: 5,5,0,3,6...
    1) [5, null, null, null, null, null]
    2) [5, null, null, null, null, null]
    3) [0, 5, null, null, null, null]
    4) [0, 3, 5, null, null, null]
    5) [0, 6, 3, 5, null, null] (output expected after inserting 6 should be 0,3,5,6...)

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: i need to add an item to my array in ascending order by using the compareTo metho

    What I suggest you do instead of having all those ifs and loops inside the add method, refactor some of the code out to other methods. For example write a method that determines at what index the new value should be added. Write a method that moves all the values from x to theSize - 1 back a slot. If you do this then all your add method does is:
    else if(contains(x) == false) {
        call method to find index to insert at
        use above index to call method to move elements
        insert new value at index
        increase theSize
        return true
    }
    return false
    By the way in your if condition use the not operator and not == false.
    else if( ! contains(x))

Similar Threads

  1. [SOLVED] Quick method/array question...?
    By kari4848 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 09:48 PM
  2. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 11:54 AM
  3. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 05:31 AM
  4. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM
  5. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM