|
-
Jan 22nd, 2002, 11:35 AM
#1
Thread Starter
Member
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)
-
Jan 22nd, 2002, 11:55 AM
#2
Hyperactive Member
//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;*/
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Jan 22nd, 2002, 01:52 PM
#3
Dazed Member
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...
Last edited by Dilenger4; Jan 22nd, 2002 at 02:05 PM.
-
Jan 23rd, 2002, 11:05 AM
#4
Hyperactive Member
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.
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|