更新方法| JAVA中的变量时出现问题
我在更新方法中的对象时遇到问题更新方法| JAVA中的变量时出现问题,java,Java,我在更新方法中的对象时遇到问题 public Admin getAdminByName(String name) throws InexistentUserException { Admin currentAdmin = null; for (Admin admin : adminList){ if (admin.getUserName().equals(name)){ currentAdmin = adm
public Admin getAdminByName(String name) throws InexistentUserException {
Admin currentAdmin = null;
for (Admin admin : adminList){
if (admin.getUserName().equals(name)){
currentAdmin = admin; //This update is still null
}
else { throw new InexistentUserException("The admin does not exist");}
}
return currentAdmin;
}
此代码始终返回null。我不需要创建一个新的对象Admin,因为我应该创建比现有对象更多的对象,我只需要按名称定位一个对象,因为您将它放在了错误的位置。现在,您的代码在列表上迭代,一旦没有找到匹配项,就会抛出异常 理想情况下,您要做的是搜索,直到找到匹配项,然后停止并返回该匹配项 在for循环之后,需要一个if语句来检查currentAdmin是否为null。如果是这样的话,你会抛出一个NoAdminExceptionFound或者类似的东西
package eu.webfarmr;
public class Admin {
private String username;
private final static Admin[] adminList = new Admin[] {new Admin("Alice"), new Admin("Bob"), new Admin("Carol") };
public static Admin getAdminByName(String name) throws InexistentUserException {
Admin currentAdmin = null;
for (Admin admin : adminList){
if (admin.getUserName().equals(name)){
currentAdmin = admin; //This update is still null
}
}
if (currentAdmin == null) {
throw new InexistentUserException("The admin does not exist");
}
return currentAdmin;
}
public Admin(String username) {
this.username = username;
}
public String getUserName() {
return this.username;
}
}
编辑-使用while以提高效率的版本
package eu.webfarmr;
导入java.util.array;
导入java.util.Iterator;
公共类管理员{
私有字符串用户名;
私有最终静态管理员[]管理员列表=新管理员[]{新管理员(“Alice”)、新管理员(“Bob”)、新管理员(“Carol”)};
公共静态管理员getAdminByName(字符串名称)引发不存在的UserException{
Admin currentAdmin=null;
布尔值=false;
迭代器迭代器=数组.asList(adminList).Iterator();
而(!found&&iterator.hasNext()){
Admin=iterator.next();
found=admin.getUserName().equals(name);
如果(找到){
currentAdmin=admin;
}
}
如果(!找到){
抛出新的不存在UserException(“管理员不存在”);
}
返回当前管理员;
}
公共管理员(字符串用户名){
this.username=用户名;
}
公共字符串getUserName(){
返回此用户名;
}
}
你扔错地方了。现在,您的代码在列表上迭代,一旦没有找到匹配项,就会抛出异常
理想情况下,您要做的是搜索,直到找到匹配项,然后停止并返回该匹配项
在for循环之后,需要一个if语句来检查currentAdmin是否为null。如果是这样的话,你会抛出一个NoAdminExceptionFound或者类似的东西
package eu.webfarmr;
public class Admin {
private String username;
private final static Admin[] adminList = new Admin[] {new Admin("Alice"), new Admin("Bob"), new Admin("Carol") };
public static Admin getAdminByName(String name) throws InexistentUserException {
Admin currentAdmin = null;
for (Admin admin : adminList){
if (admin.getUserName().equals(name)){
currentAdmin = admin; //This update is still null
}
}
if (currentAdmin == null) {
throw new InexistentUserException("The admin does not exist");
}
return currentAdmin;
}
public Admin(String username) {
this.username = username;
}
public String getUserName() {
return this.username;
}
}
编辑-使用while以提高效率的版本
package eu.webfarmr;
导入java.util.array;
导入java.util.Iterator;
公共类管理员{
私有字符串用户名;
私有最终静态管理员[]管理员列表=新管理员[]{新管理员(“Alice”)、新管理员(“Bob”)、新管理员(“Carol”)};
公共静态管理员getAdminByName(字符串名称)引发不存在的UserException{
Admin currentAdmin=null;
布尔值=false;
迭代器迭代器=数组.asList(adminList).Iterator();
而(!found&&iterator.hasNext()){
Admin=iterator.next();
found=admin.getUserName().equals(name);
如果(找到){
currentAdmin=admin;
}
}
如果(!找到){
抛出新的不存在UserException(“管理员不存在”);
}
返回当前管理员;
}
公共管理员(字符串用户名){
this.username=用户名;
}
公共字符串getUserName(){
返回此用户名;
}
}
在for循环的第一次迭代中,如果条件变为false,则将抛出异常并返回null。
您需要更改代码的抛出部分,并将其放在for循环的末尾,检查它是否未找到及其null,然后抛出异常:
public Admin getAdminByName(String name) throws InexistentUserException {
Admin currentAdmin = null;
for (Admin admin : adminList) {
if (admin.getUserName().equals(name)) {
currentAdmin = admin; // This update is still null
break;
}
}
if(currentAdmin == null) {
throw new InexistentUserException("The admin does not exist");
}
return currentAdmin;
}
在for循环的第一次迭代中,如果条件变为false,则将抛出异常并返回null。 您需要更改代码的抛出部分,并将其放在for循环的末尾,检查它是否未找到及其null,然后抛出异常:
public Admin getAdminByName(String name) throws InexistentUserException {
Admin currentAdmin = null;
for (Admin admin : adminList) {
if (admin.getUserName().equals(name)) {
currentAdmin = admin; // This update is still null
break;
}
}
if(currentAdmin == null) {
throw new InexistentUserException("The admin does not exist");
}
return currentAdmin;
}
你可以这样解决
public Admin getAdminByName(String name) throws InexistentUserException {
boolean found = false;
Admin currentAdmin = null;
for (Admin admin : adminList) {
if (admin.getUserName().equals(name)) {
currentAdmin = admin;
found = true; // This update is still null
}
}
if (found) {
return currentAdmin;
} else {
throw new InexistentUserException("The admin does not exist");
}
}
你可以这样解决
public Admin getAdminByName(String name) throws InexistentUserException {
boolean found = false;
Admin currentAdmin = null;
for (Admin admin : adminList) {
if (admin.getUserName().equals(name)) {
currentAdmin = admin;
found = true; // This update is still null
}
}
if (found) {
return currentAdmin;
} else {
throw new InexistentUserException("The admin does not exist");
}
}
如果你使用java 8或以上,你可能会考虑使用java流来解决这个问题。
Streams将允许您迭代元素并基于对象字段(在本例中为管理员名称)进行过滤。注意,它假定每个管理员都有一个唯一的名称 与您的问题相关的示例: (如果adminList是java.util.List) ''' 公共静态管理员getAdminByName(字符串名称)引发异常 { ''' 如果adminList是数组,请尝试: ''' 公共静态管理员getAdminByName(字符串名称)引发异常 {’/p>< p>如果你使用java 8或以上,你可能会考虑使用java流来解决这个问题。 Streams将允许您迭代元素并基于对象字段进行筛选,在本例中,该字段将是管理员的名称。请注意,它假定每个管理员都有一个唯一的名称 与您的问题相关的示例: (如果adminList是java.util.List) ''' 公共静态管理员getAdminByName(字符串名称)引发异常 { ''' 如果adminList是数组,请尝试: ''' 公共静态管理员getAdminByName(字符串名称)引发异常 {
'如果
adminList
为空,此方法只能返回null。为什么不直接返回admin;
而不是设置currentAdmin
变量?如果adminList
为空,此方法只能返回null。为什么不直接返回admin;
而不是设置currentAdmin
变量,感谢您的帮助。从循环中取出异常,删除局部变量currentAdmin并在循环中返回admin。除非OP专门查找最后一个匹配项,否则最简单的实现将是(admin-admin:adminList)的,如果(admin.getUserName().equals(name))返回admin;抛出新的不存在的userexception();
true,但我讨厌用return语句中断for循环,但这确实是解决的最短路径,感谢您的帮助。从循环中取出异常,删除局部变量currentAdmin并在循环中返回admin。