09-10-2012, 09:51 AM | #1 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Quick Java question
I don't know if I'm retarded or what but I can't for the life of me pass a 2 dimensional array into a method.
correct me if this is the wrong format: Code:
public class Driver { public static void main(String[] args) { char[][] hello ={{'x', 'o'}, {'o', 'x'}}; Prog call = new Prog(); call.displayArray(hello[][]); } } Code:
public class Prog { public Prog() { System.out.println("sup?"); } public void displayArray(char[][] hi) { for(int r=0; r<2; r++) { System.out.println(); for(int c=0; c<2; c++) { System.out.print(hi[r][c]); } } } } EDIT: google is surprisingly stupid when it comes to this shit. EDIT2: figured it out, apparently, you don't include the brackets when passing an array?
__________________
Last edited by rushyrulz; 09-10-2012 at 10:41 AM.. |
09-10-2012, 11:07 AM | #2 |
ツ
Join Date: Nov 2007
Location: France
Age: 34
Posts: 228
|
Re: Quick Java question
call.displayArray(hello[][]);
Replace it with call.displayArray(hello); and it should work. |
09-10-2012, 11:38 AM | #3 |
FFR Player
|
Re: Quick Java question
Why exactly does removing the brackets work though?
Knowing how arrays are stored is a large part to properly understanding them. In this case, the brackets are not necessary as an argument because the parameter requires a pointer to the array. hello[][] is trying to de-reference a specific offset of the array, but what offset?
__________________
Last edited by FissionMailed1; 09-10-2012 at 11:42 AM.. |
09-10-2012, 12:40 PM | #4 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Quick Java question
With the little knowledge that I have: the instance variable "hello" is of the type char[][], so making it hello[][] would mean it's expecting char[][][][] instead of char[][]
I think that's what's going on?
__________________
Quote:
Last edited by SKG_Scintill; 09-10-2012 at 12:46 PM.. |
|
09-10-2012, 01:39 PM | #5 |
FFR Player
|
Re: Quick Java question
If hello is a 2D array, hello[][] isn't 4D, because the bracket operator isn't used as a type declaration.
Passing hello[][] to a function is invalid Java.
__________________
Last edited by FissionMailed1; 09-10-2012 at 01:43 PM.. |
09-10-2012, 01:59 PM | #6 |
FFR Veteran
|
Re: Quick Java question
Actually FissionMailed1, Java doesn't use pointers, at least at the programming level. Internally it uses them, true, but it isn't like C++ where you can go referencing and dereferencing pointers all over the place. It hides this from the programmer much like it also hides memory management via garbage collection--sure, it does it, but you don't explicitly tell it to (although you can give it a hint--which the JVM can ignore).
The reason you don't pass hello[][] is because hello[][] is not the name of the variable. The name of the variable is hello. It has nothing to do with pointers. For example, when passing an integer to a function you don't say callsomefunction(int myinteger); but you do say callsomefunction(myinteger); Leave out type information in method/function calls. |
09-10-2012, 02:04 PM | #7 |
FFR Player
|
Re: Quick Java question
The reason I am referring to them as pointers is because I am bypassing the abstraction that Java uses to make a point regarding why the syntax works the way it does. I know you call functions without regard of the type, but as I said, I am trying to make a point.
__________________
|
09-10-2012, 02:43 PM | #8 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Quick Java question
__________________
Quote:
|
|
09-10-2012, 02:52 PM | #9 |
FFR Player
|
Re: Quick Java question
(clap), didn't see that one coming :P
__________________
|
09-10-2012, 05:24 PM | #10 | |
FFR Simfile Author
|
Re: Quick Java question
Quote:
When you're just using it as a name or passing it around - the compiler by this point already knows exactly how array-y it is, how many dimensions, how large and so on, so you don't need to tell it again. It's like how you don't say int x = 1; System.out.println(int x), it already knows it's int and you don't need to redeclare.
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|