Results 1 to 20 of 20
 04072010, 02:35 AM #1Member
 Join Date
 Apr 2010
 Posts
 28
 Rep Power
 0
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].lengthother.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;
}
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

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!
 04072010, 03:30 AM #3Member
 Join Date
 Apr 2010
 Posts
 28
 Rep Power
 0

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?
 04072010, 03:37 AM #5Member
 Join Date
 Apr 2010
 Posts
 28
 Rep Power
 0

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....
Again, best of luck!
 04072010, 03:57 AM #7Member
 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){
}
}

What errors?
 04072010, 03:59 AM #9Member
 Join Date
 Apr 2010
 Posts
 28
 Rep Power
 0
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
 04072010, 04:06 AM #11Member
 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; 04072010 at 04:11 AM.

Next, you're not calling your for each loop correctly:
Java Code:for (Matrix sumofMatrices: result){ // here is the loop body }
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);
 04072010, 04:28 AM #13Member
 Join Date
 Apr 2010
 Posts
 28
 Rep Power
 0
Lol I'm just getting confused now.. sorry man...

 04072010, 04:55 AM #15Member
 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?

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.
 04072010, 05:12 AM #17Member
 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){
result.add(sumofMatrices);
}
return result;
}

I see your problem. The Matrix class has a field called matrix that's a 2dimensional 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 foreach loop looks //.... code goes here... it uses result and aMatrix }
 04072010, 09:56 AM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,999
 Blog Entries
 7
 Rep Power
 22
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; }
Jos
 04072010, 10:49 AM #20
Similar Threads

Help with dox matrix printer
By Albert in forum Advanced JavaReplies: 7Last Post: 09062011, 08:50 AM 
scalability of matrix
By ajay kumar in forum New To JavaReplies: 1Last Post: 12072009, 09:21 AM 
Matrix Message
By shindry in forum New To JavaReplies: 2Last Post: 05042009, 04:32 AM 
displaying 2DMatrix
By srinivasmallabathula in forum New To JavaReplies: 2Last Post: 02182009, 08:19 PM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks