Results 1 to 5 of 5
  1. #1
    gaurav2211 is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default Can we generate an array for class object?

    class Box {
    double width;
    double height;
    double depth;
    Box() { // pass object to constructor
    width = 1;
    height = 2;
    depth = 3;
    }
    class OverloadCons2 {
    public static void main(String args[]) {
    ArrayList<Box> a = new ArrayList<Box>();
    for(int n=0;n<200;n++)
    {
    a.add(new Box()); //alive,score,sa[]
    }
    Here I want to create an array for the the width parameters of all the objects of type Box.

    }
    }
    Problem Statement:
    I want to create an array w[] for the the width parameters of all the objects of type Box such that
    w[0]= a.get(0).width
    w[1]= a.get(1).width
    w[2]= a.get(2).width
    ......
    Similary I want to create arrays for length height etc...There are lots of parameters in class Box . So I am wondering if there is a direct method for this kind of transformation
    I can do this with a For loop. But I have to do it repeatedly for different parameters in Box class for several 1000 times.

    Any help would be highly appreciated.
    Last edited by gaurav2211; 03-21-2009 at 04:00 AM.

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

    Default

    this begs the question: why? Why do you want to do this? What are you going to do with the array that you can't do with the already existing arraylist?

  3. #3
    gaurav2211 is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default Reason

    This is part of a big problem. So cannot type the entire code here. I want add all widths, lengths, heights seperately.

    Earlier I have created a SUM function which will take an array as argument.
    Sum(array[])

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

    Default

    Quote Originally Posted by gaurav2211 View Post
    This is part of a big problem. So cannot type the entire code here.
    didn't ask for the entire code.
    I want add all widths, lengths, heights seperately.
    so give them setters and add them in a loop. Still no need to create a separate array.

    Earlier I have created a SUM function which will take an array as argument.
    Sum(array[])
    You can create a similar method that takes the ArrayList as argument.

  5. #5
    gaurav2211 is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default Good idea

    I will try this out and will return to the thread ......It got resolved so fast:)

Similar Threads

  1. Generate a Class file
    By Daniel in forum New To Java
    Replies: 6
    Last Post: 04-20-2009, 05:37 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  4. Exception Failed to Generate Wrapper Class on WebLogic
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:15 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

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