Quick Java question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rushyrulz
    Digital Dancing!
    FFR Simfile Author
    FFR Music Producer
    • Feb 2006
    • 12985

    #1

    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]);
    			}
    		}
    	}
    }
    the error I'm getting says '.class' expected. The method program compiles, the driver does not. Pls help D:

    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, 09:41 AM.


  • Frederic54
    • Nov 2007
    • 228

    #2
    Re: Quick Java question

    call.displayArray(hello[][]);

    Replace it with call.displayArray(hello); and it should work.

    Comment

    • FissionMailed1
      FFR Player
      • Feb 2012
      • 1267

      #3
      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, 10:42 AM.


      YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

      MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

      YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

      YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

      THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

      I LOVE YOU CHEETOS.

      Comment

      • SKG_Scintill
        Spun a twirly fruitcake,
        FFR Simfile Author
        • Feb 2009
        • 3875

        #4
        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?
        Last edited by SKG_Scintill; 09-10-2012, 11:46 AM.





        Originally posted by bluguerilla
        So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
        ___
        . RHYTHMS PR LAYERING
        . ZOMG I HAD TO QUIT OUT TERRIBLE
        .

        Comment

        • FissionMailed1
          FFR Player
          • Feb 2012
          • 1267

          #5
          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, 12:43 PM.


          YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

          MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

          YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

          YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

          THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

          I LOVE YOU CHEETOS.

          Comment

          • UserNameGoesHere
            FFR Veteran
            • May 2008
            • 1114

            #6
            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.
            Originally posted by Crashfan3
            Man, what would we do without bored rednecks?

            Comment

            • FissionMailed1
              FFR Player
              • Feb 2012
              • 1267

              #7
              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.


              YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

              MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

              YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

              YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

              THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

              I LOVE YOU CHEETOS.

              Comment

              • SKG_Scintill
                Spun a twirly fruitcake,
                FFR Simfile Author
                • Feb 2009
                • 3875

                #8
                Re: Quick Java question

                Originally posted by FissionMailed1
                I am trying to make a pointer.
                Fix'd





                Originally posted by bluguerilla
                So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                ___
                . RHYTHMS PR LAYERING
                . ZOMG I HAD TO QUIT OUT TERRIBLE
                .

                Comment

                • FissionMailed1
                  FFR Player
                  • Feb 2012
                  • 1267

                  #9
                  Re: Quick Java question

                  (clap), didn't see that one coming :P


                  YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

                  MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

                  YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

                  YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

                  THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

                  I LOVE YOU CHEETOS.

                  Comment

                  • Patashu
                    FFR Simfile Author
                    FFR Simfile Author
                    • Apr 2006
                    • 8609

                    #10
                    Re: Quick Java question

                    Originally posted by rushyrulz
                    EDIT2: figured it out, apparently, you don't include the brackets when passing an array?
                    You include the brackets when you need to point out the arrayness - e.g. on declaration, construction and indexing it.
                    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

                    Comment

                    Working...