需要帮助使用arrayList对数字进行排序

时间:2016-02-29 21:27:28

标签: java arraylist

我一直在使用此计算机分配时遇到问题。基本上我们必须编辑ArrayList的两个add方法,这样它才能让我们存储从最高到最低的整数。

以下是我的教授为测试我们的添加方法而提供的代码:

package Asg3;

import java.util.ArrayList;

import myUtil.SortedArrayList;

/**
 * Don't modify any of the following codes. 
 * 
 * 2/14/2016
 * 
 * @author Chung-Chih Li
 */

public class Asg3 {



    public static void testInteger() {
        SortedArrayList<Integer> sorted= new SortedArrayList<Integer>();

        for (int i=0;i<20;i++) {
            sorted.add((int)(Math.random()*1000));
        }
        int bad=0;
        for (int i=0;i<20;i++) {
            try {
                sorted.add((int)(Math.random()*1000)%sorted.size(),(int)(Math.random()*1000));
            } catch (IllegalArgumentException e) {
                System.out.print(".");
                bad++;
            }

        System.out.println("\nsize: "+sorted.size()+"  bad insertions: "+bad);
        System.out.println(sorted.toString());}}





    public static void main(String[] args) {

        testInteger();



    }

}

以下是我的两个添加方法:

package myUtil;

public class SortedArrayList<T extends Comparable<T>>extends java.util.ArrayList<T>
{
    public SortedArrayList()
    {
        super();
    }

    public SortedArrayList(int capacity)
    {
        super();
    }

    @Override
    public boolean add(T item)
    {
        if(this.size()!=0)
        {
            int index=this.size()-1;
            //tests to see if item is greater than the last index and if so places it there
            if(item.compareTo(this.get(index))>=0)
            {
                super.add(item);
            }
            else
            {//tests to see at what index other than the last index, would be appropriate to place the item in.
                for(int i=1; i<this.size()-1;i++)
                {
                    if(item.compareTo(this.get(i-1))<=0 && item.compareTo(this.get(i+1))>=0)
                        {super.add(i,item);
                        return true;}
                    else
                        continue;
                }//fix add method
            }

        }
        else
            {super.add(0, item);}
        return true;
    }


    @Override//fix add method
    public void add(int i, T item)
    {   
        if(i==0)
        {
            if(item.compareTo(this.get(i))==0&&item.compareTo(this.get(i+1))>0)
            {
                super.add(i,item);

            }

        }
        else
        {
            try{
            if(item.compareTo(this.get(i-1))<0 && item.compareTo(this.get(i+1))>0)
            super.add(i, item); }
        catch(IndexOutOfBoundsException e){throw new IllegalArgumentException();}   
        }   





}}

现在我的程序编译但是我存储在arraylist中的整数数量很少。数组列表应该存储20+个整数。我知道我的布尔加法是问题所在。

你们给我的任何帮助都是非常感激的。

2 个答案:

答案 0 :(得分:0)

我不是100%确定这会解决您的问题,但对于初学者这段代码:

if(item.compareTo(this.get(i-1))<=0 && item.compareTo(this.get(i+1))>=0)
                        {super.add(i,item);
                        return true;}

正在比较arraylist中的每个其他元素,而不是两个相邻元素。您应该将元素i-1与i或i与i + 1进行比较,因为在i位置存在元素。

答案 1 :(得分:0)

您的add方法过于复杂。如果您知道要添加的列表已排序,那么您只需找到小于要添加的项目的第一个项目并在其之前插入。如果到达此列表的末尾并且没有任何项目较小,那么您知道需要添加到最后。无需与上下项目进行比较,也无需对空列表进行特殊检查。

for (int i = 0; i < size(); i++) {
    if (item.compareTo(get(i)) < 0) {
        add(i, item);
        return;
    }
}
add(item);

我不确定布尔返回值是什么。该方法是否应该检测重复并返回false

相关问题