-8

I just started learning java. Take a look at my code for adding 1 to each element of a 2D Array using enhanced for loop. I have also attached a image of my code in eclipse.Code for Enhanced for loop

package multi_array;

public class MainClass {

public static void main(String args[]){
    int array[][]={{1,2,3}, {4,5,6}, {7,8,9}};
    add_1(array);
}

public static void add_1(int a[][]){
    for(int[] a_row: a){
        for(int i: a_row){
            a_row[i]+=1;
        }
    }

    for(int[] a_row: a){
        for(int i: a_row){
            System.out.print(a_row[i]+"\t");
        }
        System.out.println("\n");
    }

}
}

Now when I try to run the program I get below shown error message. I have also attached the image of error message.Error message

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at multi_array.MainClass.add_1(MainClass.java:12)
    at multi_array.MainClass.main(MainClass.java:6)

How can I correct this code ?

OK friends first of all sorry for my bad indention of code, after all this is my first question on stackoverflow. I got the correct result by replacing

for(int i: a_row){
    a_row[i]+=1;
}

with the code

for(int i=0; i<a_row.length; i++){
    a_row[i]+=1;
}

But I just wanted to know if I can get the desired result by using foreach loop only.

Prashant
  • 89
  • 1
  • 1
  • 9

2 Answers2

0

a_row is containing all the rows of the two dimensional array and you are accessing a_row[i] where i value of the row and you have no a_row[3]
in your code change like following

 for(int[] a_row: a){
    for(int index=0; index < a_row.length; index++){
        a_row[index]+=1;
    }
}

for(int[] a_row: a){
    for(int i: a_row){
        System.out.print(i+"\t");
    }
    System.out.println("\n");
}
Shvet Chakra
  • 1,043
  • 10
  • 21
  • Ok I replaced a_row[i] with i only. But now I get the simple 2D array instead of adding 1 to each element of array i.e., I get numbers which I assigned to array rather than incrementing each element by 1. – Prashant Dec 17 '15 at 09:41
  • I got the correct answer by just using the normal for loop as told by you. But I just wanted to know if I can do this with the help of enhanced for loop only. – Prashant Dec 17 '15 at 09:51
  • what you were is to change the value of the array where it is placed and that can only be done by changing where objects[index] points to, which can only be done by using objects[index]. – Shvet Chakra Dec 17 '15 at 10:07
0

Your Problem is that i has a bigger value than your array has entries.

You got this problem because you are using the actual element of the array in your loop as an index.

When I take a look at your method name I think you want just to increment every integer in your array.

You can fix your code by doing that:

    for(int[] a_row: a)
    {
        for(int i:a_row)
        {
            i += 1;
        }
    }

I think you should read how foreach-loops work. There is already a good question with nice answers on SO to this topic: here.

Community
  • 1
  • 1
Felix Gerber
  • 1,615
  • 3
  • 30
  • 40
  • Ok I replaced a_row[i] with i only. But now I get the simple 2D array instead of adding 1 to each element of array i.e., I get numbers which I assigned to array rather than incrementing each element by 1. – Prashant Dec 17 '15 at 09:52