Java 与arraylist连接
这是我的密码。我知道如何使用Java 与arraylist连接,java,arraylist,concat,Java,Arraylist,Concat,这是我的密码。我知道如何使用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<
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)));
}
}
我想你只需要了解ArrayList的工作原理。您可能正在尝试这样做:
ArrayList<String> projectStrings = new ArrayList<String>();
我想你只需要了解ArrayList的工作原理。您可能正在尝试这样做:
ArrayList<String> projectStrings = new ArrayList<String>();
当前代码中的主要问题:
String
s列表。您可以通过在ArrayList
声明中添加String
generic来实现这一点,否则您将声明原始ArrayList
,因此所有元素都将被视为对象
s:
ArrayList<String> projectStrings = new ArrayList<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)));
}
}
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
另一个建议:您不需要添加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
当前代码中的主要问题:
String
s列表。您可以通过在ArrayList
声明中添加String
generic来实现这一点,否则您将声明原始ArrayList
,因此所有元素都将被视为对象
s:
ArrayList<String> projectStrings = new ArrayList<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)));
}
}
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
另一个建议:您不需要添加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
你所需要的只是
ArrayList<String> projectStrings = new ArrayList<>();
//populate as needed
System.out.println(projectStrings.get(0) + projectStrings.get(1));
ArrayList projectStrings=new ArrayList();
//根据需要填充
System.out.println(projectStrings.get(0)+projectStrings.get(1));
如果要使用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)));
}
}
注意我对泛型的使用。您定义的
ArrayList
是对象的集合。我的是字符串的集合。按自己的方式执行会消耗编译时间保证,同时也会使其他一些答案变得不可行。您所需要的只是
ArrayList<String> projectStrings = new ArrayList<>();
//populate as needed
System.out.println(projectStrings.get(0) + projectStrings.get(1));
ArrayList projectStrings=new ArrayList();
//根据需要填充
System.out.println(projectStrings.get(0)+projectStrings.get(1));
如果要使用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)));
}
}
注意我对泛型的使用。您定义的ArrayList
是对象的集合。我的是字符串的集合。按自己的方式执行会消耗编译时间保证,同时也会使其他一些答案变得不可行。System.out.println(projectStrings.get(1).concat(projectStrings.get(0))代码>?对不起,我一直在跟踪错误消息,我不知道该怎么办。@反斜杠AFAIKString
没有append
方法,即StringBuilder
和StringBuffer
@LuiggiMendoza是的,我的错误System.out.println(projectStrings.get(1).concat(projectStrings.get(0))代码>?对不起,我一直在跟踪错误消息,我不知道该怎么办。@反斜杠AFAIKString
没有append
方法,即StringBuilder
和StringBuffer
@LuiggiMendoza是的,我的错误这返回错误方法concat(java.lang.Object,java.lang.Object)类型stringProject的未定义concat
是来自String
API的方法,它应该是projectStrings.get(1).concat(projectStrings.get(0))
@BackSlashString#concat
是,但concat
本身不是。这将返回错误方法concat(java.lang.Object,java.lang.Object)类型stringProject的未定义是concat
是来自String
API的一种方法,它应该是projectStrings.get(1).concat(projectStrings.get(0))
@BackSlashString#concat
是,但单独使用concat
是不可取的。我没有注意到他没有将ArrayList声明为ArrayList。这应该能解决他的问题,说得好。我没有注意到他没有将ArrayList声明为ArrayList。这应该可以解决他的问题。@YoungDeezie请添加您当前的代码,看看错误在哪里。@YoungDeezie我已经更新了我的答案。下次请小心点,happy coding:)。@YoungDeezie请输入您当前的代码,看看错误在哪里。@YoungDeezie我已经更新了我的答案。下次请多加小心,快乐编码:)。