Java: How to store a 2D array within a 1D array -
i trying store found 2d array 1d array faster processing later. however, keep getting nullpointerexception when try fill 1d array. happens txt file has number of rows , colums read first row , column amount doing 2d array. each index reads next data element on txt file , stores @ index until 50 000 integer values stored. works fine.
now want take 2d array , store elements 1d array faster processing later when looking answers without using array list or put them in order, fine,
int [][] data = null; int[] arraycount = null; (int row = 0; row < numberofrows; row++) { (int col = 0; col < numberofcols; col++) { data[row][col] = inputfile.nextint(); } } //doesn't work gives me excpetion data[0][0] = arraycount[0];
i tried in loops no matter nullpointerexception
you haven't initialized data
, arraycount
variables, initialize follows :
int[][] data = new int[numberofrows][numberofcols]; int[] arraycount = new int[numberofrows * numberofcols];
in case, copy 2d 1d array may use :
numberofrows = data.length; if (numberofrows > 0) { numberofcols = data[0].length; } else { numberofcols = 0; } system.out.println("numberofrows : "+numberofrows); system.out.println("numberofcols : "+numberofcols); (int row = 0, count = 0; row < numberofrows; row++) { (int col = 0; col < numberofcols; col++) { arraycount[count] = data[row][col]; count++; } }
Comments
Post a Comment