返回Java中不带ArrayList的数组中的搜索结果
我在不知道可以使用ArrayList的情况下,开始在数组中实现一个简单的hw分配搜索。我意识到它有一些bug,并认为在使用ArrayList之前,我仍然会尝试知道我的bug是什么。我基本上有一个类,可以在其中添加、删除或从数组中搜索返回Java中不带ArrayList的数组中的搜索结果,java,Java,我在不知道可以使用ArrayList的情况下,开始在数组中实现一个简单的hw分配搜索。我意识到它有一些bug,并认为在使用ArrayList之前,我仍然会尝试知道我的bug是什么。我基本上有一个类,可以在其中添加、删除或从数组中搜索 public class AcmeLoanManager { public void addLoan(Loan h) { int loanId = h.getLoanId(); loanArray[loanId -
public class AcmeLoanManager
{
public void addLoan(Loan h)
{
int loanId = h.getLoanId();
loanArray[loanId - 1] = h;
}
public Loan[] getAllLoans()
{
return loanArray;
}
public Loan[] findLoans(Person p)
{
//Loan[] searchedLoanArray = new Loan[10]; // create new array to hold searched values
searchedLoanArray = this.getAllLoans(); // fill new array with all values
// Looks through only valid array values, and if Person p does not match using Person.equals()
// sets that value to null.
for (int i = 0; i < searchedLoanArray.length; i++) {
if (searchedLoanArray[i] != null) {
if (!(searchedLoanArray[i].getClient().equals(p))) {
searchedLoanArray[i] = null;
}
}
}
return searchedLoanArray;
}
public void removeLoan(int loanId)
{
loanArray[loanId - 1] = null;
}
private Loan[] loanArray = new Loan[10];
private Loan[] searchedLoanArray = new Loan[10]; // separate array to hold values returned from search
}
公共类AcmeLoanManager
{
公共贷款(贷款h)
{
int loanId=h.getLoanId();
loanArray[loanId-1]=h;
}
公共贷款[]获取所有贷款()
{
返回loanArray;
}
公共贷款[]融资贷款(p人)
{
//Loan[]searchedLoanArray=new Loan[10];//创建新数组以保存搜索到的值
searchedLoanArray=this.getAllLoans();//用所有值填充新数组
//仅查找有效的数组值,如果Person p与Person.equals()不匹配
//将该值设置为null。
for(int i=0;i
在测试时,我认为它是有效的,但我认为我在搜索后覆盖了我的成员变量。我最初认为我可以在方法中创建一个新的Loan[]并返回它,但这似乎不起作用。然后我想我可以有两个数组。一个不会更改,另一个只用于搜索的值。但是我认为我不理解一些东西,比如浅拷贝和深拷贝???…getAllLoans的返回值覆盖了searchedLoanArray引用,这意味着loanArray和searchedLoanArray都指向同一个底层数组。尝试将searchedLoanArray设置为局部变量,然后使用Arrays.copyOf。如果您试图不在作业中使用标准函数,请手动创建一个与loanArray大小相同的新Loan数组,然后循环并复制这些值。您的searchloanarray和loanArray指向同一数组。这样做
private Loan[] searchedLoanArray = new Loan[10]
什么都不做,因为你从未使用过那笔新贷款[10]
这是解决你问题的关键
searchedLoanArray = this.getAllLoans()
只需将searchedLoanArray指向loanArray您可以这样重写它:
public Loan[] findLoans(Person p)
{
Loan[] allLoans = this.getAllLoans();
System.arraycopy(allLoans, searchedLoanArray, 0, 0, allLoans.length); // fill new array with all values
// remainder of method the same
}
但就目前情况而言,该准则仍存在一些问题:
列表时,可以避免此问题
class AcmeLoanManager
{
public void addLoan(Loan l)
{
Person client = l.getClient();
List<Loan> loans = clientLoans.get(l);
if (loans==null)
{
loans = new ArrayList();
clientLoans.put(client, loans);
}
loans.add(l);
allLoans.put(l.getLoanId(), l);
}
public void removeLoan(int loanId)
{
Loan l = loans.remove(loanId);
clientLoans.remove(loan);
}
public Collection<Loan> getAllLoans()
{
return loans.values();
}
public List<Loan> findLoans(Person p)
{
List<Loan> loans = clientLoans.get(p);
if (loans==null)
loans = Collections.emptyList();
return loans;
}
private Map<Integer,Loan> allLoans = new HashMap<Integer,Loan>();
private Map<Person, List<Loan>> clientLoans = new HashMap<Person,List<Loan>>();
}
AcmeLoanManager类
{
公共贷款(贷款l)
{
Person client=l.getClient();
List loans=clientLoans.get(l);
如果(贷款==null)
{
贷款=新的ArrayList();
clientLoans.put(客户,贷款);
}
增加(l);
allLoans.put(l.getLoanId(),l);
}
公共空间移除(内部loanId)
{
Loan l=loans.remove(loanId);
客户贷款。删除(贷款);
}
公共集合getAllLoans()
{
归还贷款。价值();
}
公共名单查找人(p人)
{
List loans=clientLoans.get(p);
如果(贷款==null)
loans=Collections.emptyList();
归还贷款;
}
私有映射allLoans=newhashmap();
private Map clientLoans=新HashMap();
}
我希望这有帮助 我要做的是循环遍历这些值,并将每个值重新分配给新变量。或者,您可以使用Javaworld中描述的“深度复制”技术:您的代码中遇到了哪些bug?