这是我的代码。我知道如何使用 concat
运算符,但不知道如何使用数组或 arraylist
。
import java.util.*;
class stringProject {
static String concat(String str1, String str2) {
return str1.concat(str2);
}
public static void main(String[] args)
{
ArrayList<String> projectStrings = new ArrayList<String>();
projectStrings.add("The ");
projectStrings.add("quick ");
projectStrings.add("brown ");
projectStrings.add("fox ");
projectStrings.add("jumped ");
projectStrings.add("over ");
projectStrings.add("the ");
projectStrings.add("lazy ");
projectStrings.add("dog.");
}
System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
}
}
我的错误是:
File: C:\Users\Daniel\stringProject.java [line: 23]
Error: Syntax error on token "println", Identifier expected after this token
File: C:\Users\Daniel\stringProject.java [line: 23]
Error: Syntax error on tokens, AnnotationName expected instead
File: C:\Users\Daniel\stringProject.java [line: 23]
Error: Syntax error on token ")", delete this token
File: C:\Users\Daniel\stringProject.java [line: 23]
Error: Syntax error, insert "Type VariableDeclaratorId" to complete FormalParameter
File: C:\Users\Daniel\stringProject.java [line: 23]
Error: Syntax error, insert ")" to complete MethodDeclaration
File: C:\Users\Daniel\stringProject.java [line: 25]
Error: Syntax error on token "}", delete this token
最佳答案
当前代码中的主要问题:
您必须定义您正在使用
String
列表。您可以通过在ArrayList
声明中添加String
泛型来实现此目的,否则您将声明一个原始ArrayList
,因此所有元素都将是被视为对象
:ArrayList<String> projectStrings = new ArrayList<String>();
或 Java 7 方式,使用菱形运算符:
ArrayList<String> projectStrings = new ArrayList<>();
您尚未在代码中定义
concat
方法。也许您想使用String#concat
:System.out.println(projectStrings.get(1).concat(projectStrings(0)));
连接String
的另一种方法是使用+
符号:
System.out.println(projectStrings.get(1) + projectStrings(0));
以防万一您希望当前代码正常工作,请在 main
方法外部定义一个 concat
方法:
class stringProject {
//this method should be static in order to be used in other static methods like main
static String concat(String str1, String str2) {
//naive implementation
return str1.concat(str2);
}
public static void main(String[] args) {
//your current code here...
//since you have defined a concat method, you can use it with no problems
System.out.println(concat(projectStrings.get(1), projectStrings.get(0)));
}
}
另一个建议:你不需要添加import java.lang.*
,这是编译器默认添加的,所以你可以删除这一行。
代码编辑后,您在打印输出之前关闭了 main
方法。这就是您的代码现在的样子:
public static void main(String[] args)
{ //this is where your main method starts
ArrayList<String> projectStrings = new ArrayList<String>();
projectStrings.add("The ");
projectStrings.add("quick ");
projectStrings.add("brown ");
projectStrings.add("fox ");
projectStrings.add("jumped ");
projectStrings.add("over ");
projectStrings.add("the ");
projectStrings.add("lazy ");
projectStrings.add("dog.");
} //this is where your main method ends
//this line is outside any method, thus you get those errors
System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
} //this is where your stringProject class definition ends
解决方案:只需将 System.out.println(...)
行移到 main
方法中即可:
public static void main(String[] args)
{ //this is where your main method starts
ArrayList<String> projectStrings = new ArrayList<String>();
projectStrings.add("The ");
projectStrings.add("quick ");
projectStrings.add("brown ");
projectStrings.add("fox ");
projectStrings.add("jumped ");
projectStrings.add("over ");
projectStrings.add("the ");
projectStrings.add("lazy ");
projectStrings.add("dog.");
//easy fix
System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
} //this is where your main method ends
} //this is where your stringProject class definition ends
关于java - 与数组列表串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337069/