【问题标题】:How can I use a loop to create a new object?如何使用循环创建新对象?
【发布时间】:2011-12-05 22:49:01
【问题描述】:

所以基本上这就是我想要做的,

do{
    Object name = new Object();*Create new object*
    Object.method*run created object through a method*

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Do you wish to continue entering data");
    String answer = keyboard.nextLine();   
 } while (answer.equalsIgnoreCase("Yes"))

我需要创建数组来完成这项工作吗?如果是这样,那将如何工作?

【问题讨论】:

  • 这取决于;你想保留每个创建的对象,还是只保留最后一个这样的对象?

标签: java arrays class methods


【解决方案1】:

视情况而定。如果您的对象具有状态(例如,表示数据库中的一行),则必须为每次迭代创建它。但是如果你需要做的只是调用对象中的一个方法(即,如果它是无状态的,(没有非静态和非最终类变量或者是不可变的)),你应该只创建一个实例,然后调用该方法在每次迭代中。你的 Scanner 对象就是一个例子。您所做的只是在其中调用一个方法,因此不是每次都创建一个新对象,而是在调用该方法之前创建它,或者作为实例(类级别,理想情况下是私有)字段,以便可以重复使用在所有方法中。以下是我将如何重写您的代码:

public class MyClass {

private final Scanner scanner = new Scanner(System.in);

public void doSomething() {
    Object object = new Object();
    do {
        // call your method here
        // object.yourMethod();

        System.out.println("Do you wish to continue entering data?");
    } while (scanner.nextLine().equalsIgnoreCase("Yes"));
}

}

如果您想存储与您创建的每个实例关联的状态,那么您可以这样做:

public class MyClass {

private final Scanner scanner = new Scanner(System.in);

public void doSomething() {
    List<Object> data = new ArrayList<Object>();
    Object object;
    do {
        // call your method here
        object = new Object();
        // object.yourMethod();
        data.add(object);

        System.out.println("Do you wish to continue entering data?");
    } while (scanner.nextLine().equalsIgnoreCase("Yes"));

    for(Object d : data) {
        // do something with the info you captured
    }
}

}

【讨论】:

    【解决方案2】:

    不,如果您只是希望它运行程序(我假设方法),则不必出于任何原因保存它。

    Object name;
    Scanner scan = new Scanner(System.in);
    String answer = "";
    
    do {
        name = new Object();
        name.method();
    
        System.out.print("Do you wish to continue entering data? ");
        answer = scan.nextLine().toLowercase(); //Get the response
    } while(!answer.equals("yes"); //If they didn't enter yes then the loop stops
    

    此方法还可以节省内存,因为您不会在循环的每次迭代中创建新的 Scanner 或新内存。

    【讨论】:

      【解决方案3】:

      您可以使用ArrayList

      List<Object> names = new ArrayList<Object>();
      Scanner keyboard = new Scanner(System.in);
      String answer;
      do {
          Object o = new Object();
          o.method();
          names.add( o );
      
          System.out.println("Do you wish to continue entering data");
          answer = keyboard.nextLine();
      } while (answer.equalsIgnoreCase("Yes"));
      

      【讨论】:

        【解决方案4】:

        我会使用类似数组列表的东西。因此,您将拥有您的 do/while 并在完成您的方法所做的任何事情后,将对象添加到列表中:

        List<Object> list = new ArrayList<Object>();
        Object name;
        
        do {
        
            name = new Object();
            name.method();
        
            list.add(name);
        
        } while(answer.equalsIgnoreCase("Yes"));
        

        类似的东西。

        【讨论】:

          【解决方案5】:

          通常看起来像这样:

          Collection<Object> objectCollection = new ArrayList<Object>();  
          for(int i = 0; i < objectMax; i++)  
          {  
              Object o = new Object();  
              o.doSomething();  
              objectCollection.add(o);  
          }
          

          如果你不想存储任何东西,那就是这样:

           for(int i = 0; i < objectMax; i++)  
              {  
                  Object o = new Object();  
                  o.doSomething();  
              }
          

          【讨论】:

            猜你喜欢
            • 2012-12-27
            • 2016-02-22
            • 2020-03-29
            • 1970-01-01
            • 2019-10-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多