Results 1 to 6 of 6
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hoosierfan24 View Post
    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. #3
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by hoosierfan24 View Post
    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
    }
    --user0--

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hoosierfan24 View Post
    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. #6
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

Similar Threads

  1. Array of objects
    By Saletra in forum New To Java
    Replies: 2
    Last Post: 07-31-2010, 01:16 PM
  2. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 06:57 PM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 05:03 AM
  4. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 10:50 AM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 06:24 AM

Posting Permissions

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