1. Member
Join Date
Apr 2010
Posts
28
Rep Power
0

## Add Matices in ArrayList <Matrix>?

So I have the following method I have written:

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. 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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
Originally Posted by Fubarable
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. 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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
Originally Posted by Fubarable
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. Originally Posted by spatel14
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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
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. What errors?

9. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
It's telling me that "matrixSum[0]" must be an array type and not ArrayList..

10. Originally Posted by spatel14
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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
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 05:11 AM.

12. 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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
Lol I'm just getting confused now.. sorry man...

14. Originally Posted by spatel14
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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
Hey, can I use size() instead of length to fix the length errors I have been getting?

16. Originally Posted by spatel14
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. Member
Join Date
Apr 2010
Posts
28
Rep Power
0
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){

}
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. 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. 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++)

return result;
}```
kind regards,

Jos

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

#### Posting Permissions

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