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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:01:52+00:00 2026-05-13T13:01:52+00:00

For my current homework, I’m trying to sort my array through a generic class

  • 0

For my current homework, I’m trying to sort my array through a generic class as the user inserts values into its locations. When the size reads as fully loaded, the array class calls in an expansion method that increases the size of the array while retaining its values in proper locations, which I followed from my Professor’s note. For some reason, all my values except for location[0] seem to either be misplaced or erased from the array. I’m leaning that the problem originates in the expansion method but I have no idea how to fix this.

For example, the initial size is currently set to 5 but increments by 3 when expansion method is called. The user can input values 1,2,3,4,5 perfectly. But expansion is called when user inputs new value 6 that outputs an array of 1, 6, null, null, null, null. Any further will lead to the error “Exception in thread “main” java.lang.NullPointerException”

Here is my Sorted Array class:

public class SortedArray {
private int size;
    private int increment;
    private int top;
    Comparable[] a;

public SortedArray(int initialSize, int incrementAmount)
{
        top = -1;
        size = initialSize;
        increment = incrementAmount;
        a = new Comparable [size];
}
public int appropriatePosition(Comparable value)
{
        int hold = top;
        if(hold == -1)
        {
            hold = 0;
        }
        else
        {
            for(int i = 0; i <= top; i++)
            {
               if(value.compareTo(a[i]) > 0)
               {
                   hold = i + 1;
               }
            }
        }
        return hold;
}
public Comparable smallest()
    {
        return a[0];
    }
public Comparable largest()
    {
        return a[top];
    }
public void insert(Comparable value)// the method that my driver calls for.
{
        int ap = appropriatePosition(value);
        //Expansion if full
        if(full() == true)
        {
            expansion();
        }
        //Shifting numbers to top
        for(int i = top; i >= ap ; i--)
        {
            {
                  a[i + 1] = a[i];
            }
        }
        a[ap] = value;
        top++;

    }
public boolean full()
{
    if(top == a.length -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
public void expansion()//here's where the expansion begins
    {
        int newSize = a.length + increment;
            Comparable[] tempArray = new Comparable[newSize];
            for(int i= 0; i < a.length; i++)
            {
                tempArray[i]= a[i];
                a  = tempArray;
            }
    }

Here’s my driver class that calls for the insert method in SortedArray class.

public class IntDriver {
 public static void main(String[] args)
 {
     Scanner keyboard = new Scanner(System.in);
     //Creating variables
     int data;
     boolean check = false;
     int choice;
     int size = 5;
     int increment = 3;
     SortedArray b = new SortedArray(size, increment);
     //Creating Menu
     System.out.println("Please choose through options 1-6.");
     System.out.println("1. Insert\n2. Delete\n3. Clear\n4. Smallest\n5. Largest\n6. Exit\n7.Redisplay Menu");
     while(check == false)
     {
     choice = keyboard.nextInt();
     switch(choice)
         {
         case 1:
             System.out.println("Type the int data to store in array location.");
             data = keyboard.nextInt();
             Integer insertObj = new Integer(data);
             b.insert(insertObj);
             System.out.println("The value " + data + " is inserted");
             b.print();
            break;
  • 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-13T13:01:53+00:00Added an answer on May 13, 2026 at 1:01 pm

    In the expansion method, you’re replacing a too soon. The replacement should happen after the for loop:

    public void expansion()//here's where the expansion begins
        {
            int newSize = a.length + increment;
                Comparable[] tempArray = new Comparable[newSize];
                for(int i= 0; i < a.length; i++)
                {
                    tempArray[i]= a[i];
                }
                a  = tempArray;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not familiar with the autotools plugin, but if you look… May 14, 2026 at 8:47 pm
  • Editorial Team
    Editorial Team added an answer I think you should go for GWT JSNI - This… May 14, 2026 at 8:47 pm
  • Editorial Team
    Editorial Team added an answer The biggest advantage of a pointer-to-member or pointer-to-member-function is that… May 14, 2026 at 8:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.