0

So I got 2 methods. One which will create a multi level array and pass that and a number to my other method. My other method will then add the number to each element in the array and then my first method will print the modified array. This is my code:

Method 1:

enter code her public void exercise18d1() {
    double[][] array1 = {{2.3,6.1},{3.3,8.6},{5.4,5.2}};
    double[][] array2 = {{2.3,6.1,7.0,3.5},{3.3,8.6},{5.4,5.5,5.2}};
    increase(array1, 1.2); //plus 1 plus 2
    increase(array2, -1);//minus 1
    print(array1,5,1);
    System.out.println();
    print(array2,5,1);
}

Method 2 which modifies the array.

  public void increase(double[][] arr, double nbr) {
    for(int i = 0;i <arr.length;i++){
        arr[i][i] += nbr;

    }

The problem is with the second method. I don't have any clue on how to fix this. arr[i][i]+=nbr should mean that the element on i position should be added with nbr. So to clarify the result should be:

   3,5  7,3 
   4,5  9,8 
   6,6  6,4

   1,3  5,1  6,0  2,5
   2,3  7,6
   4,4  4,5  4,2
3
  • 1
    The ith element of arr is an entire array. Can you please clarify? Commented Nov 4, 2015 at 9:43
  • 2
    Arrays have a fixed size, you should use a List<T> or something similar for this task. Commented Nov 4, 2015 at 9:44
  • All the elements in the array should be added with nbr. The correct results should be: 3,5 7,3 4,5 9,8 6,6 6,4 1,3 5,1 6,0 2,5 2,3 7,6 4,4 4,5 4,2 Commented Nov 4, 2015 at 9:45

2 Answers 2

3

My other method will then add the number to each element in the array

That's not what it's doing at the moment. It's modifying each "diagonal" element. Imagine that you have a 5 x 10 array... you're currently only modifying 5 entries. You want nested loops - something like:

public void increaseAll(double[][] array, double amount) {
    for (double[] subArray : array) {
        for (int i = 0; i < subArray.length; i++) {
            subArray[i] += amount;
        }
    }
}

Or just using for loops:

public void increaseAll(double[][] array, double amount) {
    for (int i = 0; i < array.length; i++) {             
        for (int j = 0; i < array[i].length; j++) {
            array[i][j] += amount;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that did it! I was way off then, but I understand.
1
public void increase(double[][] arr, double nbr) {
  for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
       arr[i][j] += nbr;
    }
  }
}

This is how you can access elements of multi dimensional array.

4 Comments

I think you should replace j < 2 with arr[i].length
@AntiHeadshot: Done. Thanks :)
So you could say that i is the row and j is the element on that row?
@Baxtex: i is a row number, j is column number, arr[i][j] is the element on ith row and jth column.

Your Answer

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.