arrays and parameterized constructors?
i have the following
publicclass ClassTwo{
publicstaticvoid main(String[] args){
String[] arrayone ={"Something"};
int[] arraytwo =newint[13];
arraytwo[0] = 1;
ClassOne classOne =new ClassOne(arrayone, arraytwo);
System.out.println("Done");
}
}
AND
publicclass ClassOne{
String[] arrayone;
int[] arraytwo;
public ClassOne(String[] arrayone,int[] arraytwo)
{
this.arrayone = arrayone;
this.arraytwo = arraytwo;
}
int length = arrayone.length;
}
I get an exception when its run. If i change length in ClassOne to:
int length = 1;
Any ideas?
how exactly can i fix it?
this works:
public class ClassOne {
String[] arrayone;
int[] arraytwo;
int length;
public ClassOne(String[] arrayone, int[] arraytwo)
{
this.arrayone = arrayone;
this.arraytwo = arraytwo;
length = arrayone.length;
}
}
however i'd rather use the arrays length variable directly rather than creating un-needed variables.
here's what i mean...
public class Graph {
String memberNames[];
int releasesMade[];
int length = 0;
public Graph(String memberNames[], int[] releasesMade)
{
setMemberNames(memberNames);
setReleasesMade(releasesMade);
}
//current y position
int y_pos = 0;
//y offset to cater for header space
int headerOffset = 50;
//inner padding to make sure bars never touch the outer border
int innerOffset = 20;
//height of bar, text and total
int barHeight= 10;
int textHeight= 20;
int displayHeight = barHeight + textHeight;
//Color used for the bars
Color barColor = new Color(140,30,49);
//Set the graph's outer width
int WIDTH = 350;
//Set the graph's outer height
int HEIGHT = (releasesMade.length* displayHeight) + headerOffset + innerOffset;
//Width of the graphable area
int innerWIDTH = WIDTH - (innerOffset * 2);
//Calculate average
int average = 0;
int maximum = 0;
the variable HEIGHT needs one arrays length and that variable resides outside of the constructor.