合并两个字符串对象。JAVA

合并两个字符串对象。JAVA,java,string,list,Java,String,List,我有一个赋值要求我创建有序的字符串列表对象。 我目前有2个有序字符串列表,每个列表中有7个字符串值。 我试图创建一个方法,将列表myList和yourList合并成一个组合列表combinedList 这是我到目前为止所拥有的 public boolean merge(OrderedStringList myList, OrderedStringList yourList){ int index; String value; for (index = 0;index &l

我有一个赋值要求我创建有序的字符串列表对象。 我目前有2个有序字符串列表,每个列表中有7个字符串值。 我试图创建一个方法,将列表myList和yourList合并成一个组合列表combinedList

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

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

}
公共布尔合并(OrderedStringList myList,OrderedStringList yourList){ 整数指数; 字符串值; 对于(索引=0;索引 我在main中声明了对象combinedList,但它在orderedStringList.class中无法识别它


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

组合列表确切声明在哪里?也许您应该在方法外部声明它,以便所有方法都可以访问它

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
    }
}
公共类合并{
private OrderedStringList combinedList;//这是一个字段
私家侦探被激怒了;
公共静态void main(){
新合并()。合并(新OrderedStringList(),新OrderedStringList());
}
公共布尔合并(OrderedStringList myList,OrderedStringList yourList){
整数指数;
字符串值;
对于(索引=0;索引
如果在主函数中声明了
组合列表
,则它只能在主函数中访问


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

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

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;
}
公共布尔合并(OrderedStringList myList,OrderedStringList yourList){ 布尔结果=假; 整数指数; 对于(索引=0;索引尝试将其作为orderedStringList.class的参数传递。您是说在一个方法中声明变量,在另一个方法中尝试访问它吗?如果是这样,那是不可能的,因为这些范围不相交。您可以将您的答案标记为已接受,以表明问题已解决