0

I am making an array2, i.e. array2 = {i,j}, and I have another 2D array e_list={} . Now I want to append array2 into e_list.

     int[][] G = {{0, 0, 0, 1, 1}, 
                  {0, 0, 1, 1, 1}, 
                  {0, 1, 0, 0, 0}, 
                  {1, 1, 0, 0, 1}, 
                  {1, 1, 0, 1, 0}, 
                 }; 

     int[][]  e_list = {};  //<-initialize 2D array here

     for (int i=0; i < 5; i++){
         for (int j=0; j < 5; j++){  
             if (G[i][j] == 1){                  
                     int[] array2 = {i,j};
                     System.out.print(array2[1]); 
                     System.out.print(","); 
                     System.out.println(array2[0]);
                     //----------------------------//<-- here I want to add this array2 into that 2D array e_list   

                     //this is one of my failed try;
                     for (int t=1; t <= 6; t++){
                         for (int tt=0; tt < 1; tt++){
                             e_list[t][tt] = array2[tt];
                         }                       
                     }

         }
        }    
       }

rite now the code simply prints the 1st and 2nd indexes of array2. but I want something like this; e_list = {{3,0}, {4,0}, ... , {1,2}} I want to access the e_list like, e_list[0][0] = 3, e_list[1][0] = 4 etc

I have tried the for loops inside ... but that did not worked.it says "java.lang.ArrayIndexOutOfBoundsException: 0"

Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54
  • Your ``e_list`` is an empty array, yet you want to set a value into the first element. There is no first element in an empty array. Initialise ``e_list`` accordingly. – f1sh Nov 04 '16 at 10:37

3 Answers3

1

You need to actually tell e_list how big it is before you can assign values to it. something like int[][] e_list = new int[5][5]; instead of int[][] e_list = {};

Otherwise e_list will have no size and therefore as soon as you try to write something in it you'll get an array index out of bounds exception (your array has no size and yet you're trying to access something inside it)

Funny thing is, your //<-initialize 2D array here comment already says EXACTLY what you still need to do :D But writing comments instead of actually doing something simply isn't enough.

Mark
  • 1,498
  • 1
  • 9
  • 13
0

if you set e_list to length 5, change your loop to go until less than 5 rather less or equals 6!

for (int t=1; t < 5; t++){
  for (int tt=0; tt < 1; tt++){
    e_list[t][tt] = array2[tt];
  }                       
}
Alex
  • 483
  • 1
  • 12
  • 23
0

Try this:

public static void main(String[] args) {

        int[][] G = {{0, 0, 0, 1, 1},
        {0, 0, 1, 1, 1},
        {0, 1, 0, 0, 0},
        {1, 1, 0, 0, 1},
        {1, 1, 0, 1, 0},};

        int[][] e_list = new int[100][2];  //<-initialize 2D array here

        System.out.println("e_list size " + e_list.length);

        for (int[] a: e_list) {
            System.out.println("a: " + Arrays.toString(a));
        }


        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (G[i][j] == 1) {
                    int[] array2 = {i, j};
//                    System.out.print(array2[1]);
//                    System.out.print(",");
//                    System.out.println(array2[0]);
                    //----------------------------//<-- here I want to add this array2 into that 2D array e_list   

                    //this is one of my failed try;

                    for (int t = 0; t < e_list.length; t++) {
                        int[] tmp= e_list[t];                             
                        if (tmp[0] == 0 && tmp[1] == 0) {
                            e_list[t] = array2;
                            break;
                        }                        
                    }
                }
            }
        }       

        for (int[] a: e_list) {
            System.out.println("a: " + Arrays.toString(a));
        }

    }

Note that you need to set the proper size of the e_list beforehand. Or you can use some List and add the elements dynamically.

Nurjan
  • 5,889
  • 5
  • 34
  • 54