still learning

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rscorpio64
    New Member
    • Oct 2008
    • 5

    still learning

    here is a snipped:

    Code:
    int[][] c = new int [9][2];
    
    ...
    
    c[4][2] = -1;
    Why does that not work?
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    will you post the experimental code that makes you convinced it is not working?

    Comment

    • rscorpio64
      New Member
      • Oct 2008
      • 5

      #3
      sorry all my arrays give me an out of bounds error

      so if i have a array_example[2][9]
      and i assign array_example[2][9] = 15

      it gives me an out of bouds error

      if i change the declaraation to array_example[3][10]

      then it works!!

      why?
      There really is no program because no matter what program i use arrays in, I get this type of error. (btw I know pascal, vb (all basics) delphi, javascript and a few others so its easy for me to get the methods mixed up)

      help

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by rscorpio64
        why?
        help
        If you declare an array to have 'n' elements the valid index values are:

        0, 1, 2, 3 ... n-3, n-2, n-1 (count them all for reasonable values of 'n').

        That's why; Java always starts its indexing at element number zero,

        kind regards,

        Jos

        Comment

        • rscorpio64
          New Member
          • Oct 2008
          • 5

          #5
          Originally posted by JosAH
          If you declare an array to have 'n' elements the valid index values are:

          0, 1, 2, 3 ... n-3, n-2, n-1 (count them all for reasonable values of 'n').

          That's why; Java always starts its indexing at element number zero,

          kind regards,

          Jos
          correct - as a mater of fact the majority of programing languages start with zero on arrays

          what throws me off is for example:
          pascal theArray[1..10,1..5] wil give me a 10 x 5 array
          basic theArray(10,5) same - 10 x 5 array

          is what your saying is that the following:
          theArray[10][5]
          will NOT give me a 10x5 array (excluding the fact that it really is since your starting at zero) to the extent that I cannot reach the max theArray[10][5]= n ?

          bear in mind that the above two examples (pascal and basic) you can STILL use [0,0] (pascal) and (0,0)(basic) to use as data.

          in conclusion - so I understand java arrays - in order to achieve a 10 x 5 array (in this example) and reach those numbers (i.e. theArray[10][5] = n) it actually needs to read theArray[11][6]?

          (or I suppose I could n-- for the count.

          as an after thought, kinda defeats the purpose of making a chessboard[8][8] and only have access to 0 - 7 even though your looking at the number "8" in the dimension. lol

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by rscorpio64
            as an after thought, kinda defeats the purpose of making a chessboard[8][8] and only have access to 0 - 7 even though your looking at the number "8" in the dimension. lol
            You can't count; a board[8][8] gives you an 8x8 board indexed by the values
            0,1,2,3,4,5,6 and 7 (those are eight index values).

            kind regards,

            Jos

            Comment

            Working...