Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:04:44+00:00 2026-05-22T17:04:44+00:00

I am having trouble adding an item to my array set, it should be

  • 0

I am having trouble adding an item to my array set, it should be ordered in ascending order and for some reason I just get to add it and it does not order it.

Here is my code:

public boolean add(AnyType x){
    if(this.contains(x))
        return false;
    else if(this.isEmpty()){
        items[theSize]=x;
        theSize++;
        return true;
    }
    else{
    if( theSize == items.length )
        this.grow();
    //Here goes code for adding
    /*AnyType[] newItems = (AnyType[]) new Comparable[items.length];
    newItems = items;
    for(int i=0;i<theSize;i++)
        if(items[i].compareTo(x)>0){
            newItems[i]=x;
            newItems[i+1]=items[i];
            for(int j=i+1;j<theSize;j++)
                newItems[j]=items[i];
            items = newItems;
            theSize++;
            return true;
        }

    //*/
    items[theSize]=x; //*/
    theSize++;
    return true;
    }
}

The method should not allow an item to repeat so if you try to add something that is already in there, it should return false. If the array is empty just add to items[0] and then I tried to create a new array and once you find an item bigger than the one I’m inputing copy everything into a new array, add the new value, and just add the rest and then make items = newItems; but it did not work. I have been trying for a couple hours now so I just decided to ask for help.
I have my SortedSet class defined like this:

public class SortedSet<AnyType extends Comparable> implements Set<AnyType>
{
    private AnyType[] items;
    private int theSize;
    public SortedSet(){
        theSize = 0;
        items = (AnyType[]) new Comparable[5];
    }

I know there are other ways to do this like using TreeMap but it has to be done as an array.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:04:45+00:00Added an answer on May 22, 2026 at 5:04 pm

    I do not see where you are writing the previous values?

    Try something like this in the last else.

                theSize++;
                AnyType[] newItems = new AnyType[theSize];
                for(int i = 0; i < theSize-1; i++)
                    if(items[i].compareTo(x) > 0)
                    {
                        newItems[i] = x;
                        newItems[i + 1] = items[i];
                        for(int j = i + 2; j < theSize; j++)
                            newItems[j] = items[j-1];
                        items = newItems;
                        return true;
                    }
                    else
                        newItems[i] = items[i];
                newItems[theSize-1] = x;
                items = newItems;
                return true;
    

    Since I already coded it to make sure it works fine, here is a sample working on Strings. I focused here only on correct adding. The other issues important for the OP were omitted.

    import java.util.Arrays;
    
    public class OrderingAddTest
    {
        static int theSize = 0;
        static String[] items = new String[theSize];
        public static void main(String[] args)
        {
            add("a");
            add("e");
            add("d");
            add("b");
            add("c");
    
            System.out.println(Arrays.toString(items)+"\n; items.length="+items.length);
        }
    
        private static boolean add(String x)
        {
            theSize++;
            String[] newItems = new String[theSize];
            for(int i = 0; i < theSize - 1; i++)
                if(items[i].compareTo(x) > 0)
                {
                    newItems[i] = x;
                    newItems[i + 1] = items[i];
                    for(int j = i + 2; j < theSize; j++)
                        newItems[j] = items[j - 1];
                    items = newItems;
                    return true;
                }
                else
                    newItems[i] = items[i];
            newItems[theSize - 1] = x;
            items = newItems;
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a key value pair and having trouble adding the
I'm having some trouble with adding tips to a Request.HTML function. I have a
having a bit of trouble adding some data to a database. I have the
I'm having trouble adding Mimetypes to MimetypesFileTypeMap. I've tried adding a META-INF/mime.types file just
I'm having some trouble adding a mutable string (that has been changed with replaceOccurrencesOfString)
I am having some trouble adding a text from my MySQL database into a
I'm having trouble adding a variable amount of labels to a panel. My problem
I'm having trouble adding Facebook Connect to my iPhone Project. Here's the directory structure:
I'm having trouble adding icons to jQuery UI's buttonset's.Adding icons to buttons work fine.Does
I am disabling a form based on a checkbox... I am having trouble adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.