英文:
Can I call on different instances of a class that have the same name?
问题
对于我在课堂上正在进行的项目,我需要创建一个ArrayList,其中包含不确定数量的信徒(类的实例),它们都具有不同的名称、ID等。然后,我需要调用其中一个,例如,更改其ID(使用类似adherent.setId(newId)
的方法)。
然而,由于我的代码设置方式,所有新实例都被称为相同的名称,而且由于信徒的数量是不确定的,我不能简单地使用像下面这样的方法:
Adherent adherent1 = new Adherent(lastName, firstName, currentYear, currentId);
Adherent adherent2 = new Adherent(lastName, firstName, currentYear, currentId);
Adherent adherent3 = new Adherent(lastName, firstName, currentYear, currentId);
等等...
这是我目前正在使用的方法:
public static List<Adherent> createAdherent(int currentId, int currentYear, List<Adherent> adherentList) {
Scanner sc = new Scanner(System.in);
System.out.println("请录入新信徒的姓氏");
String lastName = sc.next();
System.out.println("请录入新信徒的名字");
String firstName = sc.next();
Adherent adherent = new Adherent(lastName, firstName, currentYear, currentId);
adherentList.add(adherent);
adherent.setName(lastName);
adherent.setFirstName(firstName);
adherent.setYearAd(currentYear);
adherent.setId(currentId);
System.out.println(adherent.toString());
return adherentList;
}
我想知道是否有一种方法可以调用它们(使用.set
或.get
方法)而不是使用它们的名称(也许是一个变量),或者是否有一种方法可以在我的createAdherent()
方法中为它们命名不同的名称。我已经搜索了几天,似乎找不到答案。
英文:
For a project I am working on in class, I am required to create an ArrayList containing an undefined number of adherents (instances of a class) which all possess different names, Id's, etc. I then have to call on one of them to, for example, change it's Id(using a method like adherent.setId(newId)
).
However, with the way my code is setup, all new instances are called the same, and because the number of adherents is undefined, I can't simply use something like
Adherent adherent1 = new Adherent(lastName, firstName, currentYear, currentId);
Adherent adherent2 = new Adherent(lastName, firstName, currentYear, currentId);
Adherent adherent3 = new Adherent(lastName, firstName, currentYear, currentId);
etc... for each of them.
This is the method I am currently using
public static List<Adherent> createAdherent(int currentId, int currentYear, List<Adherent> adherentList) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the last name of the new adherent");
String lastName = sc.next();
System.out.println("Please enter the first name of the new adherent");
String firstName = sc.next();
Adherent adherent = new Adherent(lastName, firstName, currentYear, currentId);
adherentList.add(adherent);
adherent.setName(lastName);
adherent.setFirstName(firstName);
adherent.setYearAd(currentYear);
adherent.setId(currentId);
System.out.println(adherent.toString());
return adherentList;
}
I am wondering if there is a way to call on them (use .set
or .get
method) using something other than their names (maybe a variable) or if there is a way to name them all differently in my createAdherent()
method. I have been searching for a couple days and can't seem to find an answer.
答案1
得分: 1
你的方法很有趣。我会考虑进行一些重构。
首先,你接收一个列表,向其中添加一个项目,然后返回该列表。由于你已经在列表中添加了项目,所以返回是多余的。
鉴于 createAdherent(int currentId, int currentYear, List<Adherent> adherentList)
的签名,你传入了一个列表。相反,考虑将你的方法改成:
public Adherent createAdherent(int currentId, int currentYear)
(如果必要,可以加上 static
)
然后,你可以这样调用它:
adherents.add(createAdherent(1, 2000))
其中 adherents
是你之前传入的 Adherent 列表。无需传入列表或返回列表,只需返回要添加到列表中的对象。
英文:
Your method is an interesting one. I would consider some refactoring.
First, you're taking in a List, adding an item to it, then returning that List. The return is pointless as you're already adding to the list.
Given the signature of createAdherent(int currentId, int currentYear, List<Adherent> adherentList)
, you're passing in a List. Instead, consider making your method
public Adherent createAdherent(int currentId, int currentYear)
(static if you must)
Then you would call it like this:
adherents.add(createAdherent(1, 2000))
where adherents is the List of Adherent that you were passing in. No need to take a List or return a List, just return the object that you want to add to the List.
答案2
得分: 0
根据我的理解,您想要按名称调用一个项目并更改其ID,但可能存在多个具有相同名称的项目。
请注意:我认为,如果您在构造函数中传递'lastName'和'firstName',它应该更新变量值,而不需要调用.setName(lastName)和.setFirstName(firstName)。
回到您的问题,我认为,如果您通过名称获取对象来更改ID,知道可能有多个具有相同名称的对象,这不应该发生。我认为可以这样做:
-
选项1:防止创建具有相同名称的对象,将其创建放在循环中,验证是否没有具有该名称的对象,如果没有,则将其添加到数组中。您甚至可以放置一条消息,说明已经有一个具有这个名称的对象。或者返回一个错误,由调用者在try{ createAdherent(); }catch(Exception e){...}中处理。
-
选项2:不要按名称搜索,而要按ID搜索(我认为这是搜索对象的最佳选项,因为ID只会做这个),但由于它是一个数组,您最终会遍历整个数组,这是不好的,您可以使用Map<Integer, Adherent>来通过ID获取它。
-
选项3:如果问题是您不知道哪个'Adherent'被创建,因为函数返回的是一个数组(我不建议这样命名函数),那么您可以在'createAdherent'函数内部仅返回新的'Adherent'(这有点像这是他的名字),并在必要时留下它,其他值可以通过另一个函数获取,比如:
public static List<Adherent> getAdherents(){
//如果您不希望能够从数组外部删除对象
return Collections.unmodifiableList(adherentList);
//或者简单地
return adherentList;
}
或者全部使用。
希望这有所帮助
英文:
From what I understand you want to call an item by name and change its id, but there may be more than 1 item with the same name.
Note: I believe that if you are passing 'lastName' and 'firstName' in the constructor itself, it should update the variable values, with no need to call .setName(lastName) and .setFirstName(firstName)
Returning to your problem, I believe that if you are getting the obj by name to change the id, knowing that there may be more than one with this name, I think this should not happen. What I believe can be done:
-
Op 1: Prevent the creation of an object with the same name, putting its creation in a loop, validating if there isn't one with that name and, if not, adding it to the array. You can even put a message that there is already one with this name. Or returning an error that will be handled by the caller using a try{ createAdherent(); }catch(Exception e){...}
-
Op 2: Do not search by name but by id (I believe this is the best option to search for an object, as an id would do just that), but as it is an array you would end up going through it all, which is bad, you could use a Map<Integer, Adherent> to get it by id.
-
Op 3: If the problem is that you don't know which 'Adherent' was created because the function return is an array(what i don't recomend cause the function name), then you could, inside the 'createAdherent' function, return only the new 'Adherent' (which is kinda like that's his name), and leave it in case it is necessary to get the other values, these are taken by another function, such as:
public static List<Adherent> getAdherents(){
//If you don't want to be able to delete the object from the array externally
return Collections.unmodifiableList(adherentList);
//Or simply
return adherentList;
}
Or use all then.
Hope this helps
答案3
得分: 0
你的函数 createAdherent 必须更改为 createAdherentInList,因为你在参数中提供了要更新的列表。不需要返回列表,正如前面已经说过的那样,这已经完成了。
我认为你的问题更像是一个关于拥有每个实例唯一ID的经典问题。
- 要么你决定自动生成ID,并且永远不允许外部调用更改这个ID,它将保持唯一。
- 要么这个ID可以自由更改,你无法区分一个粉丝从另一个粉丝,除非通过它被添加时的位置。你可以通过插入的顺序来检索它:Adherent myList.get(position)。
无论如何,你需要一些其他函数来检索一个粉丝,比如:
Adherent findAdherentById(someUniqueId)
list
希望对你有帮助。
英文:
Your function createAdherent must be better named createAdherentInList as you give the list to update in the parameters. And no need to return the list, as already said, it's already done.
I believe your problem is a more classic one about owning a unique ID for each instance.
- Either you decide to generate automatically the ID and never allows an external call to change this ID that will stay unique.
- Either this ID is free to be changed and you have no way to distinguish an adherent from another except by its position when it was added . You can retrieve it by it's order of insert : Adherent myList.get(position).
In any case, you need some another functions to retrieve an adherent like:
Adherent findAdherentById(someUniqueId)
list <Adherent> findAdherentByName(someName) // here returns a list as it can have homonyms and you will have to choose one before to be able to change something.
HTH
答案4
得分: 0
实例并不被称为adherent1,adherent2等等。那些只是变量的名称,它们与对象的实例只有松散的关联。
您可以从列表中获取实例,如adherentList.get(0)
等等。
您可以使用诸如for (adherent : adherentList) ...
之类的结构来处理它们。
可能有更好的组织数据的方法,但这需要更多关于您需要执行的操作的信息。
英文:
The instances are not called adherent1, adherent2, etc. Those are only the names of variables, which are only loosely associated with instances of objects.
You can get to your instances from the list as adherentList.get(0)
, etc.
You can process them all by constructs such as for (adherent : adherentList) ...
.
There might be better ways to organize the data, but that requires more infomation about what you need to do with it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论