Results 1 to 20 of 20
  1. #1
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default Add Matices in ArrayList <Matrix>?

    So I have the following method I have written:

    public Matrix add(Matrix other){

    if (other.matrix[0].length!=matrix[0].length||other.matrix.length!=matrix.length){
    return null;
    }

    Matrix ret = new Matrix(matrix.length, matrix[0].length);
    for(int i=0;i<matrix.length;i++){ //rows
    for(int j=0; j<matrix[0].length;j++){ //columns
    ret.set(i,j, matrix[i][j] + other.matrix[i][j]);
    }
    }
    return ret;
    }
    Basically, this takes a matrix entered by the user (other) and adds in to another matrix they already entered (matrix).

    However, I have to write another method called sum in which I have to add an ArrayList filled with objects of Matrix type. How can I use this method to do that? The method I need to write adds all the matrices in the ArrayList and puts them into a new array. Assume that all matrices in the ArrayList are of the same dimensions.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What would be your first guess at how to do this? Don't try to over complicate things as it's pretty straightforward.

    Best of luck!

  3. #3
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What would be your first guess at how to do this? Don't try to over complicate things as it's pretty straightforward.

    Best of luck!
    Well, here is what I wrote:

    public static Matrix sum(ArrayList <Matrix> matrixSum){
    ArrayList <Matrix> sumOfMatrices = matrixSum;

    }
    Now, I have no idea if that's how to do it..that's all I have written so far..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hint #1: Well, you have to get each Matrix out of the ArrayList and add them to a return Matrix that was created in this method for that purpose (just like add does). How would you go about getting each item in a collection? What type of code structure would you use to loop through the ArrayList?

  5. #5
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Hint #1: Well, you have to get each Matrix out of the ArrayList and add them to a return Matrix that was created in this method for that purpose (just like add does). How would you go about getting each item in a collection? What type of code structure would you use to loop through the ArrayList?
    Okay, so would a for loop work? I think it would..but I'm not quite sure how to write it..:confused: I'm sure it obvious, I just can't think of it....

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by spatel14 View Post
    Okay, so would a for loop work?
    Yep, that's what I'd use!



    I think it would..but I'm not quite sure how to write it..:confused: I'm sure it obvious, I just can't think of it....
    Regardless, you've got to try it. Go to the tutorials and see how for loops work, and then using this information, try to write one yourself. You can't break your computer in the attempt so you have nothing to lose.

    Again, best of luck!

  7. #7
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    I'm stuck..I thought I had something, but I keep getting errors..not sure what to do.

    Here is the code I wrote:

    public static Matrix sum(ArrayList <Matrix> matrixSum){

    Matrix result = new Matrix(matrixSum.length, matrixSum[0].length);
    for (Matrix sumofMatrices: result){


    }
    }
    I keep getting errors..

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What errors?

  9. #9
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    It's telling me that "matrixSum[0]" must be an array type and not ArrayList..

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by spatel14 View Post
    It's telling me that "matrixSum[0]" must be an array type and not ArrayList..
    And it's correct. You can't get an item from an ArrayList using array indices. You need to instead use the get(index) method. So, if you know that the Matrices held by the ArrayList have the same length, then you could get the 0th Matrix via:

    matrixSum.get(0), and you should be able to get the required lengths from this Matrix via the length fields:

    matrixSum.get(0).length and matrixSum.get(0)[0].length

  11. #11
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Alright that makes sense, but I'm still getting errors. Is this because I have a method called get that gets the value at the given row and column of a matrix?

    EDIT: Nvm, that shouldn't be the problem..but I can't figure out why I'm getting an error now. I checked the javadoc for ArrayList and get is definitely valid..
    Last edited by spatel14; 04-07-2010 at 04:11 AM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Next, you're not calling your for each loop correctly:
    Java Code:
    for (Matrix sumofMatrices: result){
      // here is the loop body 
    
    }
    Where does the ArrayList sumofMatrices go here? result shouldn't be in the loop condition portion of the loop like you have it but used in the loop body itself. Have you looked at the tutorial page on how to do this? If so, you'll want to have another look.

    e.g.,
    Java Code:
          String[] data = {"Fe", "Fi", "Fo", "Fum"};
    
          // don't worry about the line below...
          List<String> stringList = Arrays.asList(data);
          
          String result = "";
          
          for (String item : stringList) {
             result = result + item + " ";
          }
          System.out.println(result);

  13. #13
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Lol I'm just getting confused now.. sorry man...

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by spatel14 View Post
    Lol I'm just getting confused now.. sorry man...
    No problem. Keep at it, and it'll sink in. Guaranteed. Well, goodnight and good luck!

  15. #15
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Hey, can I use size() instead of length to fix the length errors I have been getting?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by spatel14 View Post
    Hey, can I use size() instead of length to fix the length errors I have been getting?
    size() will get you the number of Matrices in your ArrayList, but won't get you the height and width of the individual Matrix. To best help you with your errors, you'd best post your most recent code attempt, and the actual error messages along with some indication of which line is throwing what error.

  17. #17
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Alright, thanks man.

    here is the code:
    public static Matrix sum(ArrayList <Matrix> matrixSum){
    Matrix result = new Matrix(matrixSum.get(0).length, matrixSum.get(0).length);

    for (Matrix sumofMatrices: result){
    result.add(sumofMatrices);

    }
    return result;
    }
    Now, where I define Matrix result, I get an error that says "length cannot be resolved or is not a field"..how do I fix this?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I see your problem. The Matrix class has a field called matrix that's a 2-dimensional array, and you must use this field or some method to get the length.

    Java Code:
    // get first Matrix object held in the ArrayList
    Matrix firstMatrix = sumofMatrices.get(0);
    
    // try using the firstMatrix to get your height and width ...
    Matrix result = new Matrix(firstMatrix.matrix.length, firstMatrix.matrix[0].length);
    
    for (Matrix aMatrix : sumofMatrices) {  // note how a for-each loop looks
    
      //.... code goes here...  it uses result and aMatrix
    }

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    I've been reading this thread and I don't understands what the problem is: you can add two matrixes and if you have to add more of them, simply do so; if there's only one matrix in the list there's nothing to add and if there are no matrixes in the list there's nothing to do; have a look at this code snippet:

    Java Code:
    public Matrix add(List<Matrix> list) {
    
       Matrix result;
    
       if (list == null || list.size() == 0) // handle pathetic cases
         return null;
    
      result= list.get(0); // the first matrix
      for (int i= 1; 1 < list.size(); i++)
        result= result.add(list.get(i)); // add other matrixes
    
       return result;
    }
    kind regards,

    Jos

  20. #20
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    LoL
    I've been reading this thread and I don't understands what the problem is
    so did i...

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 08:50 AM
  2. scalability of matrix
    By ajay kumar in forum New To Java
    Replies: 1
    Last Post: 12-07-2009, 08:21 AM
  3. Matrix Message
    By shindry in forum New To Java
    Replies: 2
    Last Post: 05-04-2009, 04:32 AM
  4. displaying 2D-Matrix
    By srinivasmallabathula in forum New To Java
    Replies: 2
    Last Post: 02-18-2009, 07:19 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •