求java高手 帮忙解决一下问题,给定对象的具体属性值,从一个容器,取出具体对象的算法

2024-12-02 14:43:45
推荐回答(4个)
回答(1):

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
public static GeneralBean makeSure(int m, int n, List A) {
GeneralBean result = null;
for (Iterator iterator = A.iterator(); iterator.hasNext();) {
GeneralBean A1 = (GeneralBean) iterator.next();
if (A1.getM() == m && A1.getN() == n) {
result = A1;
}
}
return result;
}

public static void main(String[] args) {
GeneralBean A1 = new GeneralBean(1, 2);
GeneralBean A2 = new GeneralBean(3, 4);
ArrayList listA = new ArrayList();
listA.add(A1);
listA.add(A2);
// 这就是你要返回的对象
GeneralBean resultObject = makeSure(1, 2 , listA);
}
}

class GeneralBean {
private int m;
private int n;

public GeneralBean (int m, int n) {
this.m = m;
this.n = n;
}

public int getM() {
return m;
}
public void setM(int m) {
this.m = m;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}

回答(2):

GeneralBean A = makeSure(1,2,listA);

回答(3):

最好使用范型,容易,安全!

回答(4):

没有明白你的意思……