Results 1 to 5 of 5
- 03-21-2009, 03:56 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
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.
-
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?
- 03-21-2009, 04:12 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
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[])
-
didn't ask for the entire code.
so give them setters and add them in a loop. Still no need to create a separate array.I want add all widths, lengths, heights seperately.
You can create a similar method that takes the ArrayList as argument.Earlier I have created a SUM function which will take an array as argument.
Sum(array[])
- 03-21-2009, 04:20 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Generate a Class file
By Daniel in forum New To JavaReplies: 6Last Post: 04-20-2009, 05:37 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Exception Failed to Generate Wrapper Class on WebLogic
By christina in forum New To JavaReplies: 1Last Post: 08-07-2007, 02:15 AM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks