在没有sort命令的情况下对整数列表进行排序

时间:2014-04-23 02:18:10

标签: java

所以我有这段代码:

public class SortedIntList extends IntList
{   
    private int[] newlist;

    public SortedIntList(int size) 
    {
        super(size);
        newlist = new int[size];
    }

    public void add(int value)
    {
        for(int i = 0; i < list.length; i++)
        {
            int count = 0,
                current = list[i];

            if(current < value)
            {
                newlist[count] = current;
                count++;
            }
            else
            {
                newlist[count] = value;
                count++;
            }
        }
    }
}

然而,当我进行测试时,没有打印出来。我在同一源中的另一个类中有system.out.print。

我哪里错了?

编辑:打印评论代码:

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

EDIT2:以下评论中的超类

 public class IntList 
 { 
      protected int[] list; 
      protected int numElements = 0; 

      public IntList(int size) 
      { 
           list = new int[size]; 
      }

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

      public String toString() 
      { 
           String returnString = "";
           for (int i=0; i<numElements; i++) 
                returnString += i + ": " + list[i] + "\n"; 
           return returnString;
      }
 }

3 个答案:

答案 0 :(得分:0)

注意

这被我后来的回答所取代。我把它留在这里,而不是删除它,以防其中的信息对你有用。


两个问题。

(1)您在超类中定义list,并且可能是您打印出来的内容;但是你要将元素添加到newList,这是一个不同的字段。

(2)您只需在新列表中添加与旧列表中一样多的元素。所以你总是只有一个元素太少了。特别是,当您第一次尝试添加元素时,您的列表在添加之前和之后都没有元素。

你应该只有一个列表,而不是其中两个。此外,您应该将for循环分成两个独立的循环 - 一个循环在之前添加元素您要插入的值,第二个循环添加元素< em>在之后。

答案 1 :(得分:0)

让我们逐步了解您希望它在这里工作的逻辑:

首先创建一个新的排序列表,将10传递给构造函数,这将构成一个大小为10的整数数组。

现在你调用你的add方法将100传递给它。该方法将位置0设置为100

现在你加50,方法在位置0设置50,在位置1设置100

现在你添加200,它被放置在位置2

然后你添加25.它被设置为位置0,其他所有内容都被改组为

然后您的方法将打印出此列表中的所有内容。

所以这是你的问题:

对于第一次添加,您将初始化为0的当前值与50进行比较.0将始终小于50,因此50永远不会被设置到数组中。所有元素都是如此。

编辑:看到超类这就是你应该如何修改你的代码:

public class SortedIntList extends IntList
{   
    private int[] newlist;
    private int listSize;

    public SortedIntList(int size) 
    {
        super(size);
        // I removed the newList bit becuase the superclass has a list we are using
        listSize = 0; // this keeps track of the number of elements in the list
    }

    public void add(int value)
    {
        int placeholder;
        if (listSize == 0)
        {
            list[0] = value; // sets first element eqal to the value
            listSize++; // incriments the size, since we added a value
            return;   // breaks out of the method
        }
        for(int i = 0; i < listSize; i++)
        {
            if (list[i] > value) // checks if the current place is greater than value
            {
                placeholder = list[i]; // these three lines swap the value with the value in the array, and then sets up a comparison to continue
                list[i] = value;
                value = placeholder;
            }
        }
    list[i] = value; // we are done checking the existing array, so the remaining value gets added to the end
    listSize++;  // we added an element so this needs to increase;
    }

    public String toString() 
    { 
        String returnString = "";
        for (int i=0; i<listSize; i++) 
            returnString += i + ": " + list[i] + "\n"; 
        return returnString;
    }
}

答案 2 :(得分:0)

现在我看到了超类,它之所以永远不打印任何东西的原因很明显。 numElements始终为零。你永远不会增加它,因为你从不调用add方法的超类版本。

这意味着toString方法中的循环根本没有迭代,而toString总是只返回空字符串。