# help with and array of objects

• 01-22-2011, 09:51 PM
hoosierfan24
help with and array of objects
ok so i have and array of objects called coordinates and its size is 1000 and i want to input two random numbers into each slot. How would i set up a loop to do this?
• 01-22-2011, 10:07 PM
Fubarable
Quote:

Originally Posted by hoosierfan24
ok so i have and array of objects called coordinates and its size is 1000 and i want to input two random numbers into each slot. How would i set up a loop to do this?

Much luck.
• 01-22-2011, 10:30 PM
hoosierfan24
here is a better example of the problem

Code:

Point [] coordinates = new Point[1000];
coordinates[0] = new Point (Math.random() * 50 , Math.random() * 50);
coordinates[1] = new Point (Math.random() * 50 , Math.random() * 50);

i want to put Math.random() * 50 for the paramaters of each object in the array, is there just another way to do it without creating a 1000 different objects individually
• 01-22-2011, 10:53 PM
user0
Quote:

Originally Posted by hoosierfan24
here is a better example of the problem

Code:

Point [] coordinates = new Point[1000];
coordinates[0] = new Point (Math.random() * 50 , Math.random() * 50);
coordinates[1] = new Point (Math.random() * 50 , Math.random() * 50);

i want to put Math.random() * 50 for the paramaters of each object in the array, is there just another way to do it without creating a 1000 different objects individually

You can loop through the array using a for loop:
Code:

for(int i = 0; i < coordinates.length; i++) { // loop through each element (not "slot" as you call it)
coordinates[i] = something; // do your assignment here
}

• 01-22-2011, 10:56 PM
Fubarable
Quote:

Originally Posted by hoosierfan24
here is a better example of the problem

Code:

Point [] coordinates = new Point[1000];
coordinates[0] = new Point (Math.random() * 50 , Math.random() * 50);
coordinates[1] = new Point (Math.random() * 50 , Math.random() * 50);

i want to put Math.random() * 50 for the paramaters of each object in the array, is there just another way to do it without creating a 1000 different objects individually

You will likely use a for loop to loop through the array inserting Points into each item of the array, but this will necessarily create 1000 Point objects. I don't really see any way around that. Are you concerned with the amount of memory that this will take -- if so, I wouldn't worry. Are you confused on how to set up this for loop? Again, your question needs more detail.
• 01-23-2011, 03:45 AM
hoosierfan24
ok thanks i was able to do it that way