Thread: help with and array of objects

1. Member
Join Date
Oct 2010
Posts
45
Rep Power
0

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?

2. 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?
What do you mean by "into each slot". Your question is missing a lot of details that would help others help you. Please read the link in my signature below on how to ask smart questions.

Much luck.

3. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
ok sorry about that
here is a better example of the problem

Java 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

4. Senior Member
Join Date
Dec 2010
Posts
100
Rep Power
0
Originally Posted by hoosierfan24
ok sorry about that
here is a better example of the problem

Java 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:
Java 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
}```

5. Originally Posted by hoosierfan24
ok sorry about that
here is a better example of the problem

Java 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.

6. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
ok thanks i was able to do it that way

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•