试图运行代码,显示退出状态1 Main.java:9:错误:找不到符号 ArrayList结果= ArrayList merge(1、2); ^ 符号:变量ArrayList 位置:班级主班
和类似的错误...
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
String[] one = {"in","one","now"};
String[] two = {"here","in","two"};
ArrayList<String> result = ArrayList<String> merge(one,two);
System.out.println(result);
}
private static ArrayList<String> merge(String[] words, String[] more){
ArrayList<String> sentence = new ArrayList<String>();
for(String w:words) {ArrayList.add(w);}
for(String w:more) {ArrayList.add(w);}
return sentence;
}
}
应该返回一个包含两个字符串数组内容的ArrayList,谢谢! :)
答案 0 :(得分:0)
首先,ArrayList
没有名为static
的{{1}}方法。您想在add
上调用它。并编程到sentence
界面(您还需要导入List
);喜欢
java.util.List
此外,在private static List<String> merge(String[] words, String[] more) {
List<String> sentence = new ArrayList<>();
for (String w : words) {
sentence.add(w);
}
for (String w : more) {
sentence.add(w);
}
return sentence;
}
中只需调用main
,而无需添加更多类型信息。喜欢,
merge
上面的List<String> result = merge(one, two);
也可以用merge
编写,并与Arrays.asList(T...)
相结合,例如
Collections.addAll
答案 1 :(得分:0)
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
String[] one = {"in","one","now"};
String[] two = {"here","in","two"};
ArrayList<String> result = merge(one,two);
System.out.println(result);
}
private static ArrayList<String> merge(String[] words, String[] more){
ArrayList<String> sentence = new ArrayList<String>();
for(String w:words) {sentence.add(w);}
for(String w:more) {sentence.add(w);}
return sentence;
}
}
Console:[in,one,now,here,in,two]