-
N00b Question
This is my first week or two trying Java and I've got instructions on how to declare mutli-demensional array's but it doesn't say how to set value's to them.
Say I wanted to input from a file into an array.
What would the code be?? (to fill a mutlidemnsional array)
-
//for a 1D array
[scope] datatype variableName [] = new datatype [x];
//for a 2D array
[scope] datatype variableName [] [] = new datatype [x] [y];
/*and so on for more dimensions and where X & Y are int demensions;*/
-
Ive never had the need so far to declare a mutli-demensional array for use with a file but as CaptainPinko pointed out it can be done as
Code:
type[]identifier = new type[]
where type can either be a primative type or an object type.
Same goes for a mutli-demensional or (two dimensional array)
Code:
type[][]identifier = new type[][]
When declaring mutli-demensional arrays you must either specify row, or rows and cols. ie....... [4][] or [4][4]. Using [][4] would give you an error.
Arrays are pretty easy to work with but the manner in which they can be declared and initialized can somtimes lead to confusion. For instance....
Code:
int[]x = {0,1,2,3,4};
is the equivalent to
Code:
int[]x = new int[]{0,1,2,3,4};
Anonymous arrays can also be created, Neither the name of the array nor the dimension of the array is specified.
Code:
new int[] {0,1,2,3,4}
If you want to retrieve data from a file then your getting into I/O which consists of streams and such...
-
you can also delcared multi-demensionial arrays like that:
for example a 2D array {{1,2,3,5}, {6,7,8,9,10}}; this illustrates an important concept of Java. THERE ARE NO MUTLIDIMENSIONAL arrays. The work around is that you can have an array of arrays of data, which works rather the same way w/ the bonus that you can have ragged arrays.