简体   繁体   English

如何使用循环创建新对象?

[英]How can I use a loop to create a new object?

So essentially this is what I want to do, 所以本质上这就是我想做的

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"))

Do I need to create arrays to make this work? 我是否需要创建数组才能完成这项工作? If so how would that work? 如果是这样,那将如何工作?

Depends. 要看。 If your object has state (represents a row in a database for example), you would have to create it for each iteration. 如果对象具有状态(例如,表示数据库中的一行),则必须为每次迭代创建状态。 But if all you need to do is call a method in the object (ie if it is stateless, (has no non-static and non-final class variables or is immutable)), you should just create one instance, and call the method in each iteration. 但是,如果您只需要在对象中调用一个方法(即,它是无状态的(没有非静态和非最终类变量或不可变的)),则应该只创建一个实例,然后调用该方法在每次迭代中。 An example of that is your Scanner object. 扫描仪对象就是一个例子。 All you are doing is call a method in it, so instead of creating a new object each time, you would either create it before calling the method, or as an instance (class level, ideally private) field so it can be re-used in all methods. 您所要做的只是在其中调用一个方法,因此您不必在每次调用该方法之前先创建一个新对象,或者在实例(类级别,理想情况下为私有)字段中创建该对象,以便重新使用该对象。在所有方法中。 Here is how I would re-write your code: 这是我重新编写代码的方法:

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

} }

OTHO if you want to store the state associated with each instance you create, then you would do it like this: OTHO如果您想存储与您创建的每个实例相关联的状态,则可以这样进行:

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

} }

You could use an ArrayList . 您可以使用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"));

I would use something like an array list. 我会使用数组列表之类的东西。 So you would have your do/while and on completion of whatever your method does, add the objects to the list: 因此,您可以执行自己的操作/同时完成所有操作,然后将对象添加到列表中:

List<Object> list = new ArrayList<Object>();
Object name;

do {

    name = new Object();
    name.method();

    list.add(name);

} while(answer.equalsIgnoreCase("Yes"));

Something along those lines. 遵循这些原则。

No it would not have to be saved for any reason if you just wanted it to run a program (I assume method). 不,如果您只想运行某个程序(我假设为方法),则不必出于任何原因将其保存。

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

This method also saves you memory because you do not create a new Scanner or new memory each iteration of the loop. 该方法还可以节省您的内存,因为您不会在循环的每次迭代中创建新的Scanner或新的内存。

typically it would look like this: 通常看起来像这样:

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

If you don't want to store anything it is just this: 如果您不想存储任何东西,那就是这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM