0

I want to find the minimum and the maximum from a matrix from each column. My function

public static void findMinMax(double[][] matrix) {
 ArrayList<Double> column = new ArrayList<Double>();
 ArrayList<Double> minmax = new ArrayList<Double>();
 ArrayList<ArrayList<Double>> minMaxValues = new ArrayList<ArrayList<Double>>();
 for (int j=0;j<matrix[1].length;j++) {
 column.clear();
 minmax.clear();
 for (int i=0;i<matrix.length;i++) {
 column.add(matrix[i][j]);
 }
 double max = Collections.max(column);
 double min = Collections.min(column);
 minmax.add(min);
 minmax.add(max);
 try {
 System.out.println("adding to final minmax " + j);
 minMaxValues.add(minmax);
 System.out.println(minMaxValues);
 } catch (Exception ex) {
 System.out.println(ex.getMessage());
 }
 }
 System.out.println(minMaxValues);
 }

The problem is that at the last iteration it adds to minMaxValues only the last minmax ArrayList. Here is an example of the output

adding to final minmax 0
[[-3.5, 4.5]]
adding to final minmax 1
[[-3.5, 4.5], [-3.5, 4.5]]
adding to final minmax 2
[[-3.5, 4.5], [-3.5, 4.5], [-3.5, 4.5]]
adding to final minmax 3
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 4
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 5
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 6
[[-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2], [-3.5, 4.2]]
adding to final minmax 7
[[-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2]]
adding to final minmax 8
[[-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2]]
adding to final minmax 9
[[-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2], [-3.9, 4.2]]
adding to final minmax 10
[[-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5], [-3.9, 4.5]]

As you can see it works fine until minmax 10 where I don't understand what happens. Can anyone help me?

asked Apr 28, 2015 at 4:43
5
  • So what does matrix[1].length evaluate to? 11? Commented Apr 28, 2015 at 4:48
  • Yes, matrix has 10 rows and 11 columns Commented Apr 28, 2015 at 4:52
  • What is going wrong? your problem is not clear Commented Apr 28, 2015 at 4:52
  • @RobbyCornelissen I don't understand what you mean...Why it works fine until the last column and the adds only the last pair of minmax? Commented Apr 28, 2015 at 4:54
  • See my answer. You're just storing 11 references to the same minmax list. The contents of the list itself are overwritten in every iteration. Commented Apr 28, 2015 at 4:57

1 Answer 1

2

You need to reinitialize ArrayList<Double> minmax for every iteration. Otherwise your result list will just contain 11 references to the same list, containing the last values that you put in.

You need to move

ArrayList<Double> minmax = new ArrayList<Double>();

inside your loop.

answered Apr 28, 2015 at 4:54

2 Comments

Thanks, that was the problem. I'm still trying to understand why :)
Try to wrap your head around the fact that your minMaxValues just contains 11 references to the same minmax list. That list is in the state that you leave after the last iteration, hence containing the values of the last iteration.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.