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?

- 8,839
- 12
- 65
- 108

- 305
- 7
- 14
-
1Why can you only use a 1D array? – Octahedron Mar 30 '13 at 19:02
-
Yes only double[] array; and such no double[][] array unfortunately. – Amani Lama Mar 30 '13 at 19:03
-
1In Java multidimentional array is Array of array. – Achintya Jha Mar 30 '13 at 19:03
-
Is this a homework problem, or is there some other reason why you cannot use a 2D array? – Ben Hocking Mar 30 '13 at 19:04
-
Yes it is hmw problem, the problem is to crop an image manually with java but I do not even know how to access i and j from 1 D array... – Amani Lama Mar 30 '13 at 19:05
5 Answers
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];
}
}

- 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
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
-
It is worth noting that this is less efficient than using a 1D array. – timelmer Apr 30 '18 at 23:28
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.

- 2,511
- 3
- 19
- 25
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.

- 25
- 1
- 8