Array Constructor
I am writing a program using arrays to represent histograms. I created my class..
public class Histogram {
Now, I am looking to make a constructor so in my main method I can type...
h = new Histogram(5);
and this will create a new Histogram independent from all the other ones. I know I can type in
int[] h = new [5];
to initlialize my array, but how do I create a constructor that will do this for me each and every time.
public class Histogram {
public Histogram(int size) {......}
I need a constructor to take in an int size, and will give me a new instance of Histogram with an array of length size. Can this be done? Thank you for the help.
kschins
[721 byte] By [
kschinsa] at [2007-11-26 20:50:50]

Just put [size] in where you had [5].It didn't occur to you to try that?Here's some more info: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
i have tried that, it does not work. if my constructor is...
public Histogram (int size) {
int[] h = new int[size]; }
this only creates an instance in that constructor and is gone right away. it isn't available in my main method. if i type in
h1 = new Histogram(5);
if i type this in my main method, an array is not recognized because the constructor did not make it available everywhere. it isn't a class variable then. i am trying to make a constructor which allows me to do this. or in my main method do i need to create new arrays each time i want one. maybe this is more clear of what i am looking for. thank you though.
kschins
Sorry, I don't really clear with your question.
You want to make an array of Histogram ?
Then why don't you create this in your main method :
Histogram[] h = new Histogram[size];
for (int i=0 ; i<size ; i++)
{
Histogram[i] = new Histogram();
}
>
class H {
int[] someting;
public H(int size) {
// note, no "int" in front, else we have a local variable that hides
//the member variable.
something = new int[size];
}
}
Okay, I appreciate the help, indeed. It is quick. I am trying to explain, but this is tough to explain. I am doing an assignment creating arrays. I must write a program where the user does not know anything about arrays. I am to write a constructor, and a few methods which "change" the arrays, but this part is not the problem thus far. I am able to create new arrays as often as possible in my main method. Here is my code...
public class Histogram {
public Histogram(int size) {
int[] h = new int[size];
}
public static void main(String args[]) {
h1 = new Histogram(5);
System.out.println(h1.length);
}
}
In my main method, I create h1 thinking it will create an array of size 5 using the constructor Histogram which takes in an integer size, and in this case size is 5. But when I try to system.out.println(h1.length), the Java compiler does not find h1. This is because the constructor is only creating arrays within the constructor and they are not available in the main method. I need to write a constructor that allows me to do this. Maybe I am thinking about this the enitrely wrong way. I can type in
int[] h1 = new int[5];
this will create my intended array, but i have to assume the user does not know this command. i need to assume they want to make a histogram of size 5 so they type in..
h1 = new Histogram(5);
I sure hope this is much clearer, if not, then I must have no idea what I am doing. Thank you again.
kschins
With your code, you are not creating an array in main method. Thus, printing the h1.length will produce compile error.
Maybe you can try my code that I have posted above, and see
whether it asnwer your requirement or not.
From that code, the user can enter the size, and you get the size, then create the Histogram array in your main method.
Or I misunderstood your question ?
Message was edited by:
Thunder_Blast
ok thank you jverd...i will try this, i didnt see this post before i posted the last one.and then in my main method i should be able to typeh1 = new Histogram(5); and this will create an array which I can then System.out.println(h1.length);correct?
> ok thank you jverd...i will try this, i didnt see
> this post before i posted the last one.
>
> and then in my main method i should be able to type
>
> h1 = new Histogram(5);
>
> and this will create an array which I can then
>
> System.out.println(h1.length);
>
> correct?
Again, with this, you cannot print h1.length, since h1 is not an array at all. h1 is an Object of your Histogram.
If you want to print the length, maybe in Histogram class, you can create a method :
public int getLength()
{
return size
}
and to print in your main method, instead of using h1.length, u change it to :
System.out.println(h1.getLength());