【问题标题】:Java: Expanding array size, can't seem to keep all values in original locationsJava:扩展数组大小,似乎无法将所有值保留在原始位置
【发布时间】:2010-01-23 00:05:00
【问题描述】:

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

例如,初始大小当前设置为 5,但在调用扩展方法时会增加 3。用户可以完美地输入值 1,2,3,4,5。但是当用户输入新值 6 时会调用扩展,该值会输出 1、6、null、null、null、null 的数组。任何进一步将导致错误“线程“主”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 类中的插入方法。

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;

【问题讨论】:

    标签: java arrays generic-programming expansion


    【解决方案1】:

    在扩展方法中,您太快替换了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;
        }
    

    【讨论】:

    • 啊,我明白了,如果“a = tempArray”是否属于 for 循环,我的教授的笔记不太清楚。谢谢
    • 只是你需要在逻辑上了解自己的部分;)
    • 哈哈,真的。想一想——如果你正在制作一个对象的副本,在你完成将所有元素复制到新的 (tempArray) 之前,你不想覆盖原来的 (a)。跨度>
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2016-01-13
    • 2020-02-14
    • 2023-03-05
    相关资源
    最近更新 更多