Using a put() method in arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmmtacos
    New Member
    • Apr 2008
    • 3

    Using a put() method in arrays

    Hey,
    I'm pretty new to Java and am having some trouble writing a put() method for one of my programs. The basic idea of it is to have a matrix, made by a multi-dimensional array, then to create the method with three arguements, the row, the column and the object to place into this array.

    To declare the array, and the two objects I have this
    Code:
    	int [] [] matrixArray = new int [5] [5];
    	
    	
    	Object a = "a";
    	Object b = "b";
    Then I have a method declaration that looks like this (with x being row, y being column)

    Code:
    public void put(int x, int y, Object object) {
    
      =matrixArray(x, y, object); // This clearly doesn't work as it hasn't even been fully filled out, but is along the lines of what I'm thinking!
    
    }
    I understand that I would need to have something like,
    matrixArray.put (1, 3, a)
    to insert the object, but I am really lost at where to start with writing this method. Any help would be appreciated!

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by mmmtacos
    Hey,
    I'm pretty new to Java and am having some trouble writing a put() method for one of my programs. The basic idea of it is to have a matrix, made by a multi-dimensional array, then to create the method with three arguements, the row, the column and the object to place into this array.

    To declare the array, and the two objects I have this
    Code:
    	int [] [] matrixArray = new int [5] [5];
    	
    	
    	Object a = "a";
    	Object b = "b";
    Then I have a method declaration that looks like this (with x being row, y being column)

    Code:
    public void put(int x, int y, Object object) {
    
      =matrixArray(x, y, object); // This clearly doesn't work as it hasn't even been fully filled out, but is along the lines of what I'm thinking!
    
    }
    I understand that I would need to have something like,
    matrixArray.put (1, 3, a)
    to insert the object, but I am really lost at where to start with writing this method. Any help would be appreciated!

    Thanks
    1.) An int[] can only store integers.
    2.) To store the integer 5 an int[] called array at the second position you use
    [CODE=java]array[1] = 5;[/CODE]

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Your matrixArray object needs access to some real matrix; why not make that
      matrix a member of the MatrixArray class?

      [code=java]
      public class MatrixArray {
      private Object[][] m;
      public MatrixArray(int n, int m) {
      m= new Object[n][m];
      }
      Object get(int i, int j) { return m[i][j]; }
      ...
      // your put(Object o, int i, int j) method here
      }
      [/code]

      This is most of the proposed class: the constructor gets the dimensions of the
      matrix and builds a private matrix. I already did the get() method for you. You
      do the put() method.

      kind regards,

      Jos

      ps. Maybe the Decorator pattern article I wrote a while ago is a nice read.

      Comment

      • mmmtacos
        New Member
        • Apr 2008
        • 3

        #4
        Originally posted by r035198x
        1.) An int[] can only store integers.
        2.) To store the integer 5 an int[] called array at the second position you use
        [CODE=java]array[1] = 5;[/CODE]
        Hey,
        Thanks for the reply [=

        I understand this, but how would I integrate the put() method into that line of code?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by mmmtacos
          Hey,
          Thanks for the reply [=

          I understand this, but how would I integrate the put() method into that line of code?
          You'd need a class for it (all methods must belong to a class in Java) and that's when the class suggested by Jos above comes in.

          Comment

          • mmmtacos
            New Member
            • Apr 2008
            • 3

            #6
            Ohhhh that makes alot more sense now haha
            Sorry JosAH I missed your post before

            So using this I can do something more like, m.put() instead, which would make alot more sense!

            Thanks for your help guys. I'll have a read of the article because my next task will be using generics!

            Comment

            Working...