# Add Matices in ArrayList <Matrix>?

• 04-07-2010, 02:35 AM
spatel14
So I have the following method I have written:

Quote:

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
• 04-07-2010, 03:28 AM
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!
• 04-07-2010, 03:30 AM
spatel14
Quote:

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:

Quote:

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..
• 04-07-2010, 03:33 AM
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?
• 04-07-2010, 03:37 AM
spatel14
Quote:

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....
• 04-07-2010, 03:47 AM
Fubarable
Quote:

Originally Posted by spatel14
Okay, so would a for loop work?

Yep, that's what I'd use!

Quote:

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!
• 04-07-2010, 03:57 AM
spatel14
I'm stuck..I thought I had something, but I keep getting errors..not sure what to do.

Here is the code I wrote:

Quote:

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

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

}
}
I keep getting errors..
• 04-07-2010, 03:58 AM
Fubarable
What errors?
• 04-07-2010, 03:59 AM
spatel14
It's telling me that "matrixSum[0]" must be an array type and not ArrayList..
• 04-07-2010, 04:03 AM
Fubarable
Quote:

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
• 04-07-2010, 04:06 AM
spatel14
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..
• 04-07-2010, 04:14 AM
Fubarable
Next, you're not calling your for each loop correctly:
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.,
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);```
• 04-07-2010, 04:28 AM
spatel14
Lol I'm just getting confused now.. sorry man...
• 04-07-2010, 04:31 AM
Fubarable
Quote:

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!
• 04-07-2010, 04:55 AM
spatel14
Hey, can I use size() instead of length to fix the length errors I have been getting?
• 04-07-2010, 05:09 AM
Fubarable
Quote:

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.
• 04-07-2010, 05:12 AM
spatel14
Alright, thanks man.

here is the code:
Quote:

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?
• 04-07-2010, 05:25 AM
Fubarable
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.

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 }```
• 04-07-2010, 09:56 AM
JosAH
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:

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
• 04-07-2010, 10:49 AM
programmer_007
LoL
Quote:

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