Java:扩展数组大小,似乎无法将所有值保留在原始位置

发布于 2024-08-19 09:29:11 字数 3088 浏览 4 评论 0原文

对于我当前的作业,当用户将值插入其位置时,我尝试通过通用类对数组进行排序。当大小读取为完全加载时,数组类会调用一个扩展方法,该方法会增加数组的大小,同时将其值保留在适当的位置,这是我从教授的笔记中遵循的。由于某种原因,除了 location[0] 之外的所有值似乎都被放错位置或从数组中删除。我认为问题源于扩展方法,但我不知道如何解决这个问题。

例如,初始大小当前设置为 5,但在调用扩展方法时会增加 3。用户可以完美输入值1、2、3、4、5。但是当用户输入新值 6 时,会调用扩展,该值输出 1, 6, null, null, null, null 的数组。任何进一步的操作都会导致错误“线程“main”java.lang.NullPointerException 中出现异常”

这是我的排序数组类:

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;
            }
    }

这是我的驱动程序类,它调用 SortedArray 类中的 insert 方法。

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2024-08-26 09:29:12

在扩展方法中,您过早地替换了 a。替换应该发生在 for 循环之后:

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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文