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?

[1534 byte] By [spear_arrowa] at [2007-11-27 8:30:03]
# 1

You get the exception because length is a instance variable. Just becuase you placed it after the constructor doesn't mean it will get executed after the constructor is called. Therefore when you attempt to assign a value to length you are trying to get the length of the array which has no value.

floundera at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

spear_arrowa at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 3
Then don't create another variable. Everytime you want to use the arrays length, access it directly. ieif(arrayone.length > 0) {// do something}
floundera at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 4
but i need it for a subsequent variablei.e.int calculateRoute = someArray.length + 2;
spear_arrowa at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 5

and your question is?

What is wrong with that. It is certainly allowed. Or if you are still concerned about burning the memory with unnecessary variables then you can still use the array's length directly.

if((array.length + 2) != 10) {

}

floundera at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

spear_arrowa at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 7
I don't understand what is going on in this thread at all.
cotton.ma at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...
# 8

Is all that code supposed to inside a method(s)?

So what's stopping you?

class Graph {

String[] memberNames[];

int height;

public Graph(........) {

memberNames = ....;

height = memberNames.length;

}

// code

}

floundera at 2007-7-12 20:20:33 > top of Java-index,Java Essentials,Java Programming...