0

我有一个任务要求我制作有序的字符串列表对象。我目前有 2 个有序字符串列表,每个列表中有 7 个字符串值。我试图创建一种方法,将列表 myList 和 yourList 合并为组合列表 combineList。

这是我到目前为止所拥有的。

public boolean merge(OrderedStringList myList, OrderedStringList yourList){
    int index;
    String value;
    for (index = 0;index < numUsed; index++){
        value = myList.storage[index];
        combinedList.insert(value);
    }
    for (index = 0;index < numUsed; index++){
        value = yourList.storage[index];
        combinedList.insert(value);
    }

}

我在我的 main 中声明了对象 combineList,但它在我的 orderedStringList.class 中无法识别它

insert 函数将按字母顺序插入字符串。

4

3 回答 3

0

具体在哪里combinedList声明?也许您应该在您的方法之外声明它,以便所有方法都可以访问它。

public class Merger {
    private OrderedStringList combinedList; // This is a field
    private int numUsed;
    public static void main(){
        new Merger().merge(new OrderedStringList(),new OrderedStringList());
    }
    public boolean merge(OrderedStringList myList, OrderedStringList yourList){
        int index;
        String value;
        for (index = 0;index < numUsed; index++){
            value = myList.storage[index];
            combinedList.insert(value);
        }
        for (index = 0;index < numUsed; index++){
            value = yourList.storage[index];
            combinedList.insert(value);
        }
        return false;
    }
}
class OrderedStringList {
    public String[] storage;
    public void insert(String value) {
        // TODO Auto-generated method stub
    }
}
于 2013-02-21T02:42:15.420 回答
0

如果您combinedList在主函数内部声明,则只能在主函数内部访问。

您应该做的是combinedList在上述函数内部创建并将结果返回给调用函数,即您的main函数。

于 2013-02-21T02:44:13.190 回答
0

了解如何解决我的问题。我只是根据每个列表中的字符串数量调用了插入函数,并将它们添加到新的组合列表中。

public boolean merge(OrderedStringList myList, OrderedStringList yourList) {
    boolean result = false;
    int index;
    for (index = 0; index < myList.numUsed; index++) {
        Insert(myList.storage[index]);
        result = true;
    }
    for (index = 0; index < yourList.numUsed; index++) {
        Insert(yourList.storage[index]);
        result = true;
    }
    return result;
}
于 2013-02-21T03:53:42.633 回答