私は配列にオブジェクトを追加するメソッドを持っていますが、それはオーダーに基づいてそれを追加します。だから私はリンゴとコーラを持っている配列を持っていると言うことができます。もし私がバナナを追加したいのであれば、その2つのオブジェクトの間に行きます。しかし、jUnitテストを実行すると、エラーが発生します。ここでは、コードは次のようになります。注文に基づいて配列に何かを追加します
/**
* Adds an item to the list. This method assumes that the list is already
* sorted in alphabetical order based on the names of the items in the list.
*
* The new item will be inserted into the list in the appropriate place so
* that the list will remain alphabetized by names.
*
* In order to accomodate the new item, the internal array must be re-sized
* so that it is one unit larger than it was before the call to this method.
*
* @param itemToAdd refers to a Listable item to be added to this list
*/
public void add(Listable itemToAdd) {
int i;
Listable[] newItems = new Listable[items.length+1];
for(i=0; i<items.length;i++){
if(items[i].getName().compareTo(itemToAdd.getName()) < 0){
newItems[i] = items[i];
} else{
break;
}
}
int str=i;
for(i=str+1;i<items.length;i++){
newItems[i+1] = items[i];
}
newItems[str] = itemToAdd;
}
私はテストが< 14を期待>が、< 0を得たことを示すエラーを取得しておく>今、私はそれがその私のコンストラクタは、問題のthats意味だと思う:
/**
* This constructor creates an empty list by creating an internal array
* of size 0. (Note that this is NOT the same thing as setting the internal
* instance variable to null.)
*/
public SortedListOfImmutables() {
int i = 0;
items = new Listable[0];
//orders the list
for(i=0;i<items.length;i++){
if(Food.FOOD_OBJECTS[i].getName().compareTo(Food.FOOD_OBJECTS[i+1].getName()) < 0){
items[i] = Food.FOOD_OBJECTS[i];
}else{
items[i+1] = Food.FOOD_OBJECTS[i];
}
}
}
:
かを:あなたはどちらかやりたい、これを修正するには、インデックス3 でnewItemsに(「オレンジ」)の項目にインデックス2にするものですここにジュニット?また、私は 'items'が空の配列でサイズがゼロであることに興味があり、ループ内に何かを追加します。何か不足していますか? – Wizard
そして、なぜテストは "14"を期待していますか?どのテスト、それは何ですか?あなたはあなたを助けることが非常に難しいです。 – Meier