a private Array:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khajeddin
    New Member
    • Nov 2006
    • 51

    a private Array:

    hi:im beginner in Java and have learned C before.Now i have to write a
    "Tic Tac Toe "game and completely confused what to do in Java and classes and Methods and so on!!!!
    one of my problems i this program is:
    i have to write a private 3-by-3 two-dimentional array but i have some Compiler Error when write:
    private int array[][]=new int[3][3];
    whats the problem.
  • BSCode266
    New Member
    • Jan 2007
    • 38

    #2
    Code:
    int[][] Array = new int[5][];
    
    Array[0] = new int[2];
    Array[1] = new int[2];
    Array[2] = new int[4];
    Array[3] = new int[8];
    Array[4] = new int[3];
    What error are you exactly getting?
    I figure ur getting a nullpointer exception because you still need to fill the spots.

    BSCode266

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by khajeddin
      hi:im beginner in Java and have learned C before.Now i have to write a
      "Tic Tac Toe "game and completely confused what to do in Java and classes and Methods and so on!!!!
      one of my problems i this program is:
      i have to write a private 3-by-3 two-dimentional array but i have some Compiler Error when write:
      private int array[][]=new int[3][3];
      whats the problem.
      Technically what you've written is correct. Java consensus prefers the following
      notation:
      Code:
      private int[][] array= new int[3][3];
      This shows a bit more that the type is a 2 dimensional int array: int[][].

      My guess is that you tried to make a *local* array private which doesn't
      make sense of course. Show some more context around that array definition.

      kind regards,

      Jos

      Comment

      • khajeddin
        New Member
        • Nov 2006
        • 51

        #4
        i have public method and i want to make a private array in it.but when i write:
        Code:
        public void getnumber(){
                    private int[][] array=new int[3][3];
                     ............}
        i recieve an error,when i erase PRIVATE error disapears.
        finaly i wrote this code out of the method and in the begining of my class!!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by khajeddin
          i have public method and i want to make a private array in it.but when i write:
          Code:
          public void getnumber(){
                      private int[][] array=new int[3][3];
                       ............}
          i recieve an error,when i erase PRIVATE error disapears.
          finaly i wrote this code out of the method and in the begining of my class!!
          So my guess was right: local variables are "private" to the methods after all so
          there's no need to make them "private" explicitly. The keyword "private" only
          applies to class members (whether static or not).

          If you don't need that matrix outside of your method don't make it a member
          variable just to circumvent that compiler diagnostic message.

          kind regards,

          Jos

          Comment

          • khajeddin
            New Member
            • Nov 2006
            • 51

            #6
            thanks alot.

            Comment

            Working...