创建下一个对象并将其放入 ArrayList
Creating next object and placing it in an ArrayList
好的,所以我觉得解释我的意思可能非常困难,但是,我会尝试。
所以,基本上我想做的是 CREATE 下一个 OBJECT 和 PLACE 它在 ARRAYLIST 使用 USER INPUT.
是否可以使用ArrayList的计数来实现?
假设我的构造函数允许传递一个名称以创建一个对象,我是否可以向用户询问该名称并稍后使用它来这样做,然后将它传递给已经创建的 ArrayList?
我觉得我无法正确解释我的意思,英语不是我的母语,抱歉。 :)
public class Voter
{
private String name;
private ArrayList<Voter> voters;
public Voter(){
this.name = "";
voters = new ArrayList();
}
public Voter(String name)
{
this.name = name;
voters = new ArrayList<>();
}
public void add(Voter v)
{
if (!this.voters.contains(v))
{
this.voters.add(v);
}
}
}
public static void main(String[] args)
{
Voter v1 = new Voter("Admin");
Voter v = new Voter();
v.add(v1);
displayMenu();
int option = getInt("Enter Option:", 0, 3);
while (option != END)
{
if (option == 1)
{
doOption1();
int count = v.countArray(v);
}
}
}
public static void displayMenu()
{
System.out.println("\nSample Menu of Options");
System.out.println("0. Exit this menu");
System.out.println("1. Option 1");
}
public static void doOption1()
{
System.out.println("Please, enter the below details to register for
voting system.");
System.out.println("Full name");
String name = keyboard.nextLine();
//THIS IS WHERE I WANT TO CREATE A NEW OBJECT AND PUT IT AS AN ELEMENT OF
//MY Voter ARRAYLIST
}
public static int getInt(String prompt, int min, int max)
{
System.out.print(prompt);
int value = keyboard.nextInt();
while ((value < min) || (value > max))
{
System.out.println("Invalid - [" + min + "," + max + "] only");
System.out.print(prompt);
value = keyboard.nextInt();
}
keyboard.nextLine();
return value;
}
使用您当前的算法,要将项目添加到 Voter
对象的 ArrayList
,您所要做的就是首先重建 [=] 的方法定义15=] 到下面的方法。
public static void doOption1(Voter voter){
System.out.println("Please, enter the below details to register for voting system.");
System.out.println("Full name");
String name = keyboard.nextLine();
voter.add(new Voter(name)); // you could validate name before constructing the object if you see fit.
}
完成后,您需要更改此代码:
while (option != END){
if (option == 1){
doOption1();
int count = v.countArray(v);
}
}
对此:
while (option != END){
if (option == 1){
doOption1(v); // notice the argument
int count = v.countArray(v);
}
}
好的,所以我觉得解释我的意思可能非常困难,但是,我会尝试。 所以,基本上我想做的是 CREATE 下一个 OBJECT 和 PLACE 它在 ARRAYLIST 使用 USER INPUT.
是否可以使用ArrayList的计数来实现? 假设我的构造函数允许传递一个名称以创建一个对象,我是否可以向用户询问该名称并稍后使用它来这样做,然后将它传递给已经创建的 ArrayList?
我觉得我无法正确解释我的意思,英语不是我的母语,抱歉。 :)
public class Voter
{
private String name;
private ArrayList<Voter> voters;
public Voter(){
this.name = "";
voters = new ArrayList();
}
public Voter(String name)
{
this.name = name;
voters = new ArrayList<>();
}
public void add(Voter v)
{
if (!this.voters.contains(v))
{
this.voters.add(v);
}
}
}
public static void main(String[] args)
{
Voter v1 = new Voter("Admin");
Voter v = new Voter();
v.add(v1);
displayMenu();
int option = getInt("Enter Option:", 0, 3);
while (option != END)
{
if (option == 1)
{
doOption1();
int count = v.countArray(v);
}
}
}
public static void displayMenu()
{
System.out.println("\nSample Menu of Options");
System.out.println("0. Exit this menu");
System.out.println("1. Option 1");
}
public static void doOption1()
{
System.out.println("Please, enter the below details to register for
voting system.");
System.out.println("Full name");
String name = keyboard.nextLine();
//THIS IS WHERE I WANT TO CREATE A NEW OBJECT AND PUT IT AS AN ELEMENT OF
//MY Voter ARRAYLIST
}
public static int getInt(String prompt, int min, int max)
{
System.out.print(prompt);
int value = keyboard.nextInt();
while ((value < min) || (value > max))
{
System.out.println("Invalid - [" + min + "," + max + "] only");
System.out.print(prompt);
value = keyboard.nextInt();
}
keyboard.nextLine();
return value;
}
使用您当前的算法,要将项目添加到 Voter
对象的 ArrayList
,您所要做的就是首先重建 [=] 的方法定义15=] 到下面的方法。
public static void doOption1(Voter voter){
System.out.println("Please, enter the below details to register for voting system.");
System.out.println("Full name");
String name = keyboard.nextLine();
voter.add(new Voter(name)); // you could validate name before constructing the object if you see fit.
}
完成后,您需要更改此代码:
while (option != END){
if (option == 1){
doOption1();
int count = v.countArray(v);
}
}
对此:
while (option != END){
if (option == 1){
doOption1(v); // notice the argument
int count = v.countArray(v);
}
}