所以我在完成这项家庭作业时遇到了麻烦。基本上我们必须创建扩展java.util.ArrayList的类SortedArrayList,并覆盖ArrayList的默认添加方法,以便我们可以维护从最低到最高的整数排序列表。
以下是我的教授给全班测试方法的代码:
package Asg3;
import java.util.ArrayList;
import myUtil.SortedArrayList;
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();
}
}
这是我到目前为止覆盖add方法以便它们维护一个已排序的arraylist。
package myUtil;
public class SortedArrayList<T extends Comparable<T>>extends java.util.ArrayList<T>
{
public SortedArrayList()
{
}
public SortedArrayList(int capacity)
{
}
@Override
public boolean add(T item)
{
int index=this.size()-1;
//checks to see if item is greater than or equivalent to the last element in the list
if(item.compareTo(this.get(index))>=0||this.get(index)==null)
{
//creates a new element at the end of the list and sets the value of item to it
this.set(index+1,item);
}
return true;
}
@Override
public void add(int i, T item)
{
//check to see if item is greater than the previous element, and less than the next element
if(item.compareTo(this.get(i-1))<0 && item.compareTo(this.get(i+1))>0)
this.set(i, item);
}
}
我收到错误说:线程中的异常&#34; main&#34; ArrayIndexOutOfBoundsException:-1
at myUtil.SortedArrayList.add(SortedArrayList.java:21)
SortedArrayList的第21行是boolean add方法中的if语句。
如果这似乎是一个愚蠢的问题,很抱歉发布,但我的教授在过去两天的公布时间内没有出现,所以我真的没有其他地方可以去救命。一如既往地感谢大家的回复。
答案 0 :(得分:2)
问题是你从现有的ArrayList中检索'previous'和'next'元素以进行比较,而不检查这些元素是否确实存在。需要考虑的特殊情况是:清单是空的;在第一个元素之前插入,或在最后一个元素之后添加。
尝试插入通过排序检查后,您可以使用super.add(value)
和super.add(position, value)
进行实际操作。据我所知,没有必要在任何时候使用set()方法。
(由于这是作业,我对提供(伪)代码犹豫不决,至少没有给你机会自己思考特殊情况。)
答案 1 :(得分:1)
我认为问题来自于列表最初为空时所以index = -1。在这种情况下,一种可能的解决方案是在if语句中包围整个事物:
if (this.size()!=0)
{
... //Your code goes here
}
else this.set(0, item);
编辑:
事实证明,问题还有更多。这是我的最终代码;希望你能通读它并理解我改变了什么。 (除此之外,我还为特殊情况添加了测试。)
这应该显示如下:
size: 21 bad insertions: 19 //This should always add up to 40, because an add() method is called 40 times in total by testInteger()
[64, 65, 155, 281, 284, 340, 363, 444, 529, 596, 600, 628, 665, 745, 782, 856, 872, 885, 891, 899, 996]
package myUtil;
@SuppressWarnings("serial")
public class SortedArrayList<T extends Comparable<T>>extends java.util.ArrayList<T>
{
public SortedArrayList() {}
public SortedArrayList(int capacity) {}
@Override
public boolean add(T item)
{
int index=this.size();
//Adds item to every position until it finds one that works
for (int i=0;i<=index;i++)
{
try
{
add(i,item);
break;
}
catch (IllegalArgumentException e) {}
}
return true;
}
@Override
public void add(int i, T item)
{
//Tests for special cases with if statements.
//If item is inserted into the wrong place, throws IllegalArgumentException()
if (i<=size())
{
if (size()==0) super.add(item);
else if (i==size())
{
if(item.compareTo(this.get(i-1))>=0)
super.add(i, item);
else throw new IllegalArgumentException();
}
else if (i==0)
{
if(item.compareTo(this.get(i))<=0)
super.add(i, item);
else throw new IllegalArgumentException();
}
else
{
if(item.compareTo(this.get(i-1))>=0&&item.compareTo(this.get(i))<=0)
super.add(i, item);
else throw new IllegalArgumentException();
}
}
else
{
throw new IllegalArgumentException();
}
}
}