How to Create Multiple Objects Through Loops in Java

Creating multiple objects with different names in a loop to store in an array list

ArrayList<Customer> custArr = new ArrayList<Customer>();
while(youWantToContinue) {
//get a customerName
//get an amount
custArr.add(new Customer(customerName, amount);
}

For this to work... you'll have to fix your constructor...


Assuming your Customer class has variables called name and sale, your constructor should look like this:

public Customer(String customerName, double amount) {
name = customerName;
sale = amount;
}

Change your Store class to something more like this:

public class Store {

private ArrayList<Customer> custArr;

public new Store() {
custArr = new ArrayList<Customer>();
}

public void addSale(String customerName, double amount) {
custArr.add(new Customer(customerName, amount));
}

public Customer getSaleAtIndex(int index) {
return custArr.get(index);
}

//or if you want the entire ArrayList:
public ArrayList getCustArr() {
return custArr;
}
}

Creating multiple objects using a loop

With a couple of exceptions, local variables in Java are scoped to the nearest surrounding set of braces. That means, as far as the compiler is concerned, the variable no longer exists once you exit the braces. The variable oneCar declared on the first iteration doesn't exist by the time you reach the second iteration. It is equivalent to writing:

{
Car oneCar = new Car();
}
{
Car oneCar = new Car();
}

which is perfectly legal.

How to create multiple object in loop to keep remember each device uniquely

well then, it'll be more or less like this.

Device[] d = new Device[100]; //declare
for(int i=0;i<100;i++)
{
d[i] = new Device(); //initilize
d[i] = Graph.create(parameters); //store
}

Same goes for Link.

Link[] l= new Link[100]; //declare
for(int i=0;i<100;i++)
{
l[i] = new link(parameter); //initialize and store
}

btw, is new link(parameter); a typo? Instead, you meant to write new Link(parameter) ;?

Adding multiple objects with loop for java

try to put

in.nextLine();

after

int limiter = in.nextInt();

in your code. The first one in your code is taking just "" as input.

Creating multiple objects using a for loop

You can do this using array:

int n = 10;
Driver[] driverArray = new Driver[n];
for(int i = 0 ; i < n; i++){
driverArray[i]= new Driver();
}

In your code, you are declaring a local reference to Driver class, and creating new Driver object in every iteration.


It doesn't work for two reasons:

  1. You are declaring local reference in for-loop so the only place, when you can use it is this for-loop.

  2. Even if you declare the reference outside the loop you would initialize it with new Driver object so after loop you would have the only one Driver insance - the last one.


For more about arrays you can read here.


Hope it helps.

Creating multiple Java objects inside Loop and using all outside

You made the properties in test2 static, this means all instances share the same properties. So when you change them for the 2nd row, the 1st row changes as well.

Remove the 'static' from s1 and s2, and from your printEncLoc() method and your code works.

EDIT: See https://www.baeldung.com/java-static for more on how static works

Creating multiple objects in java with for loop using the same object name to understand the finalize method

Also, why does finalize execute for 10 million but not for 1000 if we
are constantly creating and discarding the same object

You really create 10 million distinct objects in this applications's lifetime, each of which gets referenced by o , one after the other.

Each time, the de-referenced one becomes eligible for garbage collection, because it has become unreachable from the application, and the garbage collector then calls it's finalize method.

How to create multiple objects using for loop and array

1) Initialize array of Resource ids

 int[] ids = {R.id.spinner_grade_1,R.id.spinner_grade_2};
Spinner[] spinners = new Spinner[ids.length];


for(int i=0 ;i< ids.length;i++) {
spinners[i] = (Spinner) findViewById(ids[i]);
}


Related Topics



Leave a reply



Submit