2016-02-29 46 views
-1

我不断遇到此计算机赋值问题。实质上,我们必须编辑ArrayList的两个添加方法,以便它可以让我们存储从最高到最低的整数。需要帮助使用arrayList排序数字

这里是我的教授提供测试我们添加方法代码:

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





}} 

现在我的程序编译,但整数I的量已经存储在arraylist是少数几个。数组列表应该存储20个以上的整数。我知道我的布尔添加方法是问题。

任何帮助你们可以为我提供的是一如既往的赞赏。

+3

调试器是你的朋友!浏览你的代码,看看发生了什么。 –

回答

0

您正在过度简化您的add方法。如果您知道要添加的列表已排序,则只需找到第一个小于要添加并插入的项目的项目。如果你到达这个列表的末尾,并且没有一个项目变小,那么你知道你需要添加到最后。没有必要与上面和下面的项目进行比较,并且不需要对空列表进行特殊检查。

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

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

+0

是的,我明白你的意思了,我明确表示它过于复杂,谢谢你的帮助。至于布尔返回值,我们的老师只是想大声笑,老实说不知道为什么。我们要重写类ArrayList中的聋人添加方法,无论出于何种原因,其中一个是布尔类型,并始终返回true。谢谢你的帮助人。 – skulltula

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 + 1,因为在位置i我已经有了一个元素。