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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:38:52+00:00 2026-05-27T05:38:52+00:00

i have problem arranging the numbers from small to big. Im trying to overide

  • 0

i have problem arranging the numbers from small to big. Im trying to overide the method of intlist in sortedinlist, the output i get is 0, 0,0. how can i fix this?

run:
0: 100
1: 50
2: 200
3: 25

0: 0
1: 0
2: 0

SortedIntList.java

public class SortedIntList extends IntList

{

    public SortedIntList(int Size)
    {
        super(Size);
    }

    public void add(int value)
    {
    if (numElements == list.length)
        System.out.println("Can't add, list is full");

    else{
        int empty = 0; 
         int f;
         for(f=0;f<list.length-1;f++){
                if(list[f] == 0)
            {empty = f;}
        }

        list[empty] = value;

        numElements++;    


    }
    int k = 0; int temp = 0;
    for(int i=0;i<list.length-1;i++)
        {
        k = i;
        for(int j = i+1;   j < list.length; j++)
        {
            if(list[j] < list[k])
                k = j;
        }

        temp = list[k];
        list[k] = list[i];
        list[i] = temp;
    }
        }
    }

ListTest.java

public class ListTest {
public static void main(String[] args)
{
IntList myList = new IntList(10);
myList.add(100);
myList.add(50);
myList.add(200);
myList.add(25);
System.out.println(myList);

SortedIntList myList2 = new SortedIntList(10);
myList2.add(20);
myList2.add(100);
myList2.add(30);
System.out.println(myList2);

}
}

IntList.java

public class IntList {

protected int[] list;
protected int numElements = 0;
//-------------------------------------------------------------
// Constructor -- creates an integer list of a given size.
//-------------------------------------------------------------
public IntList(int size)
{
list = new int[size];
}
//-------------------------------------------------------------
// Adds an integer to the list. If the list is full,
// prints a message and does nothing.
//-------------------------------------------------------------
public void add(int value)
{
if (numElements == list.length)
System.out.println("Can't add, list is full");
else
{
list[numElements] = value;
numElements++;
}
}

//-------------------------------------------------------------
// Returns a string containing the elements of the list with their
// indices.
//-------------------------------------------------------------
public String toString()
{
String returnString = "";
for (int i=0; i<numElements; i++)
returnString += i + ": " + list[i] + "\n";
return returnString;
}

 }
  • 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-27T05:38:53+00:00Added an answer on May 27, 2026 at 5:38 am

    Your problem is that you add the new numbers at the end of the total list. See this code (slightly reformatted)

    int empty = 0;  
    for(int f=0;f<list.length-1;f++){
        if(list[f] == 0) {
            empty = f;
        }
    }
    
    list[empty] = value;
    

    Say your list is empty but can have 5 elements. This is what happens

    Check position 0 is 0. Ok, set empty to 0.
    Check position 1 is 0. Ok, set empty to 1.
    Check position 2 is 0. Ok, set empty to 2.
    Check position 3 is 0. Ok, set empty to 3.
    Check position 4 is 0. Ok, set empty to 4.
    As end of list, insert new value at "empty" position (4).
    Increase the actual length by 1.
    

    However, if you now print your list it only prints the first element which is 0 since the inserted element is at the end of the list.

    You probably want to break out of your loop early or nay check for actual number of elements, not how many elements could be in the list.

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

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have problem while sending messages from android to PC. Messages are send when
Have problem while getting data from Memcached on .NET MVC solution. I have this
I have problem calling a JavaScript function in an iframe from the parent page.
I have problem, when converting Windows application from .NET2.0 to .NET3.5. I do this
I have problem when creating a file in encoding 'utf-8' and reading it from
I have problem in Listview onListItemClick() method. I am not getting the position of
I have problem with this method in NLog library: NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(object state) It consume too

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.