【问题标题】:Creating next object and placing it in an ArrayList创建下一个对象并将其放置在 ArrayList 中
【发布时间】:2017-08-31 19:52:35
【问题描述】:

好吧,所以我有一种感觉,解释我的意思可能非常困难,但是,我会尝试。 所以,基本上我想做的是CREATE下一个OBJECTPLACE使用ARRAYLIST ARRAYLIST strong>用户输入。

是否可以使用 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;
}

【问题讨论】:

  • 好的,所以您希望允许用户通过用户输入定义对象属性,然后将构造的对象添加到 ArrayList 中?
  • 首先,您需要一个 SCANNER 来读取 USER INPUT,然后您需要 STORE VARIABLE 中的 USER INPUT,或直接将其添加到 ARRAYLIST
  • 确实不清楚你在问什么。最好的办法:展示你当前的代码并解释你卡在哪里。首先阅读“如何创建minimal reproducible example”。
  • 我想使用用户的输入(在本例中为名称)作为构造函数的参数创建一个全新的对象,然后将这个新创建的类实例添加到 ArrayList。
  • @G.Martyna 您必须至少提供一些代码才能让人们帮助您。代码当然不必工作,但至少提供您尝试过的内容。

标签: java class object arraylist instances


【解决方案1】:

在您当前的算法已经到位的情况下,要向Voter 对象的ArrayList 添加项目,您只需首先将doOption1 的方法定义重构为以下方法。

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);
    }
}

【讨论】:

  • 太棒了!我明白你来自哪里。现在,如果我的 Voter 类中有更多字段,我基本上只需要在这一行添加更多参数 voter.getVoters().add(new Voter(name));正确的?正如我所说,这只是我代码的一小部分,所以我意识到其中有很多缺失的部分。
  • @G.Martyna 是正确的,如果您的构造函数定义中有更多参数,那么您必须添加更多参数。
  • 非常感谢!它确实解决了我的问题! :)
  • 我还有一个问题(我知道已经有一段时间了)。如果我想保存这个新选民并将其保留在我的数组列表中以供其他时间使用怎么办?现在它将投票者添加到数组列表中,但是每当我关闭应用程序并重新启动它时,我刚刚创建的新投票者就消失了。
  • 非常感谢!我确实有 SQL 的基本经验,但我想我会使用后一种解决方案!非常感谢,再次感谢! :)
猜你喜欢
  • 2022-12-08
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 2015-05-27
  • 1970-01-01
相关资源
最近更新 更多