用specefic结构构建列表的java问题
我对Java非常陌生,我有一个小问题。我有一个返回用specefic结构构建列表的java问题,java,list,Java,List,我对Java非常陌生,我有一个小问题。我有一个返回字符串的函数和一个返回int的函数,我必须将它们放入一个列表中,看起来像这样({String,int},{String,int})。我最好怎么做?如果您的问题是@duffymo在评论中描述的,那么我认为Java不会这样做。但是您可以通过创建一个包含两个字段的类来解决这个问题 private String methodForString() { String result = new String(); // ... your co
字符串的函数
和一个返回int
的函数,我必须将它们放入一个列表中,看起来像这样({String,int},{String,int})
。我最好怎么做?如果您的问题是@duffymo在评论中描述的,那么我认为Java不会这样做。但是您可以通过创建一个包含两个字段的类来解决这个问题
private String methodForString() {
String result = new String();
// ... your code which manipulates result
return result;
}
private int methodForInt() {
int result = 0;
// ... your code which manipulates result
return result;
}
class MyClass {
String returnString;
int returnInt;
public String getReturnString() {
return returnString;
}
public void setReturnString(String returnString) {
this.returnString = returnString;
}
public int getReturnInt() {
return returnInt;
}
public void setReturnInt(int returnInt) {
this.returnInt = returnInt;
}
}
private void initializingMethod() {
List<MyClass> list = new ArrayList<>();
int numberItems = 100;// this represents the number of items you want to
// put in your list
for (int i = 0; i < numberItems; i++) {
MyClass myClass = new MyClass();
myClass.setReturnInt(methodForInt());
myClass.setReturnString(methodForString());
list.add(myClass);
}
}
private String methodForString(){
字符串结果=新字符串();
//…处理结果的代码
返回结果;
}
私有int methodForInt(){
int结果=0;
//…处理结果的代码
返回结果;
}
类MyClass{
字符串返回字符串;
返回int;
公共字符串getReturnString(){
返回字符串;
}
公共void setReturnString(String returnString){
this.returnString=returnString;
}
public int getReturnInt(){
返回returnInt;
}
public void setReturnInt(int returnInt){
this.returnInt=returnInt;
}
}
私有void初始化方法(){
列表=新的ArrayList();
int numberItems=100;//这表示要删除的项目数
//列入你的名单
对于(int i=0;i
如果您的问题是@duffymo在其评论中所描述的,那么我认为Java不会这样做。但是您可以通过创建一个包含两个字段的类来解决这个问题
private String methodForString() {
String result = new String();
// ... your code which manipulates result
return result;
}
private int methodForInt() {
int result = 0;
// ... your code which manipulates result
return result;
}
class MyClass {
String returnString;
int returnInt;
public String getReturnString() {
return returnString;
}
public void setReturnString(String returnString) {
this.returnString = returnString;
}
public int getReturnInt() {
return returnInt;
}
public void setReturnInt(int returnInt) {
this.returnInt = returnInt;
}
}
private void initializingMethod() {
List<MyClass> list = new ArrayList<>();
int numberItems = 100;// this represents the number of items you want to
// put in your list
for (int i = 0; i < numberItems; i++) {
MyClass myClass = new MyClass();
myClass.setReturnInt(methodForInt());
myClass.setReturnString(methodForString());
list.add(myClass);
}
}
private String methodForString(){
字符串结果=新字符串();
//…处理结果的代码
返回结果;
}
私有int methodForInt(){
int结果=0;
//…处理结果的代码
返回结果;
}
类MyClass{
字符串返回字符串;
返回int;
公共字符串getReturnString(){
返回字符串;
}
公共void setReturnString(String returnString){
this.returnString=returnString;
}
public int getReturnInt(){
返回returnInt;
}
public void setReturnInt(int returnInt){
this.returnInt=returnInt;
}
}
私有void初始化方法(){
列表=新的ArrayList();
int numberItems=100;//这表示要删除的项目数
//列入你的名单
对于(int i=0;i
您应该为此创建bean类或使用集合API。使用HashMap]aMap
或List
。我不知道我是否理解这个问题,但Java中没有像Python中那样的元组。将这两个值封装在某种类型的对象中,并将它们添加到列表中。感谢快速asnwers,我想我想使用List
,但我如何才能获得列表的特定结构?您应该为此创建bean类或使用集合API。使用HashMap]aMap
或List
。i我不知道我是否理解这个问题,但是Java中没有像Python中那样的元组。将这两个值封装在某种对象中,并将它们添加到列表中。感谢快速asnwers,我想我希望使用List
,但如何才能获得列表的特定结构?