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 3879296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:42:37+00:00 2026-05-19T22:42:37+00:00

There seems to be a problem in add method of the class I have

  • 0

There seems to be a problem in add method of the class I have written.. I want to make a SortedList using an array, but I can’t figure out what the problem is. This is my code:

public class SortedList {

    private Integer[] elements;
    private int size;
    private int capacity;

    public SortedList(int cap) {

        elements = new Integer[cap];

        if (cap > 0)
        {
            cap = capacity;
        }
        else
            capacity = 10;

    }

    public boolean isEmpty()
    {
        return size == 0;
    }

    public boolean isFull()
    {
        return size == capacity;
    }

    public int size()
    {
        return size;
    }

    public void doubleCapacity()
    {
        capacity = capacity * 2;
    }

    public void add(Integer el)
    {
        if(this.isEmpty())
        {
            elements[0] = el;
            size++;
        }

        else if(this.isFull())
        {
            this.doubleCapacity();
            for(int i = 0; i<this.size(); i++)
            {
                if(el >= elements[i])
                {
                    elements[i+2] = elements[i+1];
                    elements[i+1] = el;
                }

                else
                {
                    elements[i+1] = elements[i];
                    elements[i] = el;
                }
            }
            size++;
        }
        else
        {
            for(int i = 0; i<this.size(); i++)
            {
                if(el >= elements[i])
                {
                    elements[i+2] = elements[i+1];
                    elements[i+1] = el;
                }
                else
                {
                    elements[i+1] = elements[i];
                    elements[i] = el;
                }
            }
            size++;
        }

    }

    public String toString()
    {
        String s = "";
        s = s + "<SortedList[";
        for(int i = 0; i < this.size(); i++)
        {
            s = s + elements[i];
            if(i < this.size()-1)
                s = s + ",";
        }
        s = s + "]>";
        return s;
    }


    public static void main(String[] args)
    {
        SortedList sl = new SortedList(5);
        sl.add(3);
        //sl.add(2);
        sl.add(4);
        sl.add(5);
//      sl.add(6);
        System.out.println(sl.toString());
    }



}

My code works if I only add 2 Integers to my list, but when I try to add the numbers 3,4,5 then I get 3,5,5…

What can be the problem? 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-19T22:42:37+00:00Added an answer on May 19, 2026 at 10:42 pm

    public class SortedList {

    private Integer[] elements;
    private int size=0;
    private int capacity;
    
    public SortedList(int cap) {
    
        elements = new Integer[cap];
    
        if (cap > 0)
        {
            capacity = cap;
        }
        else
            capacity = 10;
    
    }
    
    public boolean isEmpty()
    {
        return size == 0;
    }
    
    public boolean isFull()
    {
        return size == capacity;
    }
    
    public int size()
    {
        return size;
    }
    
    public void doubleCapacity()
    {
        capacity = capacity * 2;
    }
    
    public void add(Integer el) throws Exception{
        elements[size] = el;
        size++;
        if(size>capacity){
            throw new Exception("Size Exceeded");
        }
    }
    
    public String toString()
    {
        sort();
        String s = "";
        s = s + "<SortedList[";
        for(int i = 0; i < this.size(); i++)
        {
            s = s + elements[i];
            if(i < this.size()-1)
                s = s + ",";
        }
        s = s + "]>";
        return s;
    }
    
    public void sort(){
        for (int i=0; i <size()-1; i++) {
            if (elements[i] > elements[i+1]) {
                // exchange elements
                int temp = elements[i];
                elements[i] = elements[i+1];
                elements[i+1] = temp;
            }
        }
    }
    
    public static void main(String[] args)
    {
        try {
            SortedList sl = new SortedList(5);
            sl.add(3);
            //sl.add(2);
            sl.add(6);
            sl.add(5);
    

    // sl.add(6);
    System.out.println(sl.toString());
    } catch (Exception ex) {
    Logger.getLogger(SortedList.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be a problem when virtualenv is used in PowerShell. When I
Hi i encountered this problem whereby when i initialized my String[], there seems to
Seems there's nothing to do… using the combo prototype/lowpro, no problems at all with
There are a ton of questions regarding this problem but none of them seem
There seems to be a similar question to this on here but with the
There seems to be too many attributes/parameters in CSS... I want to know all
I have two classes: Action class, that has a method for executing VBScript files,
I have an InputStream that is set up to handle local files, but want
I have a performance-intensive iPhone game I would like to add sounds to. There
I have a priority queue implementation in C# that I want to add a

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.