5

I was used to Matlab's feature where you can make a matrix and get A[i][j] and things like that. Now I am using Java and we can only use one dimensional array. I am suppose to modify the entries (i:for rows and j:for columns) using a nested for loop but I am not sure how to access them if they are stored in an 1D array. Can someone please help me out? How difficult is it?

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Amani Lama
  • 305
  • 7
  • 14

5 Answers5

7
 int rows = 3;
 int cols = 4;
 int[] array = new int[rows*cols];
 int[] currentRow = new int[cols];
 for (int i = 0; i < rows; ++i) {
     for (int j = 0; j < cols; ++j) {
         currentRow[j] = array[i*cols + j];
     }
 }
Rob G
  • 3,496
  • 1
  • 20
  • 29
  • It worked great, thank you and everyone else. Could someone please tell me what my question got a -1? I would like to avoid such things, so I would like to know what I did wrong.... – Amani Lama Apr 01 '13 at 14:34
  • this logic made my day 'array[i*cols + j]'. Previously, I need to use a variable to store the 'columnCount++' and a 'if-else' to check columnCount is less than the input array length. Thanks @RobG – sky91 Dec 20 '15 at 14:19
5
private int getElem(int[] arr, int i, int j){
  return arr[i*colNum+j];
}
gkiko
  • 2,283
  • 3
  • 30
  • 50
1

Note that in Java, you can also use a 2D array like so:

int[][] my2DArr = new int[4][3]; //creates a 2D array with 4 rows and 3 columns
int value = my2DArr[2][1]; //gets the value at row 2 and column 1

Now, if you have to use a 1D array which is representing a 2D array, you can do some simple math to find out the position of a given row, column pair if you know the number of columns and number of rows. See here: Convert a 2D array into a 1D array

Community
  • 1
  • 1
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
1

I hope I'm understanding this correctly.

Say you have a 10x10 2D array and you want it to be one dimensional.

You can make array[0] to array[9] the first row of the 2D array. Then array[10] to array[19] is the second row of the 2D array.

There is probably a more efficient way of doing this.

Steven Morad
  • 2,511
  • 3
  • 19
  • 25
1

All arrays the computer keeps track of are 1D because of sequential memory locations in which they are stored (C Programming: A Modern Approach - K.N.King). So if you want to use a 1D array like a 2D array, simply do this: (example using String array)

String str[] = {"a", "b", "c", "d", "e", "f"};

which is like:

String str[][] = {{"a", "b", "c"}, {"d", "e", "f"}};

So you can specify how many rows and columns there are manually so in the above str[][] there are 2 rows and 3 columns or any other permutation of the numbers (1 and 6, 3 and 2, 6 and 1). Simply write your code like this:

int rows = 2;
int cols = (str.length / rows);
for (int i = 0; i < rows*cols; i+=cols)
    for (int j = 0; j < cols; j++)
        System.out.println(str[(i + j)]);

and replace the println() with whatever code you want and i+j will always be the row and column location as long as your rows variable is how many rows you are working with.

user2005078
  • 25
  • 1
  • 8