PDA

View Full Version : Java: Multi-dimensional arrays, able to pass columns vs rows?


gust0208
06-29-2005, 05:33 AM
Hello everyone,

I am first time poster and did a search and did not find an answer to this question and not sure if this functionality is built into the Java API.

I am coding an algorithm that relies heavily on working on 2-dimensional arrays. It is easy to pass the rows of the matrix with the "array of arrays" notion, but not sure if it is possible to pass the columns of the matrix.

Code example is:
int[][] aMatrix = new int[4][4];
changeArray(aMatrix[0]); //passes first row of 2-dimensional array

If not a built in functionality, what would be the best way to add this functionality to my program? Creating a separate multi-dimensional array where the rows would be pointers to the values contained in the respective columns?

Cheers,
Tom

gust0208
06-29-2005, 05:58 AM
Hello everyone,

Here is my first attempt at handling this problem. My algorithm uses a 2-dimensional array of int's, so I created a simple class called "IntObj" that looks like:

class IntObj {
public int value;
}

So instead of int[][] newarray, I use IntObj[][] newarray, and this allows me to pass or reference each value in the 2-dimensional array as a pointer! And I can make up a 2-dimensional array that references the column value pointers and pass that to my various functions. It seems to work well, but I am open to any more elegant solutions to my problem.

Cheers,
Tom

gust0208
06-29-2005, 06:48 AM
Hi everyone,

As I think about this more, it is becoming more straightforward and it made me come up with a good general class for my algorithm and one that might be useful for others working with 2 dimensional arrays. Here is the code for IntObj and IntObjArray classes:

class IntObj {
public int value;
}

class IntObjMatrix {
// [row][col] as a rule for the matrix variable.
private IntObj[][] matrix;
private IntObj[][] rows;
private IntObj[][] cols;

public IntObjMatrix(int num_rows, int num_cols) {
matrix = new IntObj[num_rows][num_cols];
int z = 0;
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
matrix[i][j] = new IntObj();
matrix[i][j].value = i + j + z;
}
z++;
}

cols = new IntObj[num_rows][num_cols];
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
cols[i][j] = matrix[j][i];
}
}

rows = new IntObj[num_rows][num_cols];
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
rows[i] = matrix[i];
}
}
}

public IntObj[] getRow(int get_row) { return rows[get_row];}
public IntObj[] getCol(int get_col) { return cols[get_col];}
public IntObj get(int get_row, int get_col) { return matrix[get_row][get_col];}
public void set(int set_row, int set_col, int value) { matrix[set_row][set_col].value = value;}
public void printmatrix() {
System.out.println();
System.out.println("Printing TestObjMatrix");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j].value + " ");
}
System.out.println();
}
}
public void printrows() {
System.out.println();
System.out.println("Printing Rows");
for (int i = 0; i < rows.length; i++) {
for (int j = 0; j < rows[i].length; j++) {
System.out.print(rows[i][j].value + " ");
}
System.out.println();
}
}
public void printrow(int k) {
System.out.println();
System.out.println("Printing Row " + k);
for (int i = 0; i < rows.length; i++) {
System.out.print(rows[k][i].value + " ");
//System.out.println();
}
System.out.println();
}
public void printcols() {
System.out.println();
System.out.println("Printing Cols");
for (int i = 0; i < cols.length; i++) {
for (int j = 0; j < cols[i].length; j++) {
System.out.print(cols[i][j].value + " ");
}
System.out.println();
}
}
public void printcol(int k) {
System.out.println();
System.out.println("Printing Col " + k);
for (int i = 0; i < rows.length; i++) {
System.out.print(cols[k][i].value + " ");
//System.out.println();
}
System.out.println();
}
}

Pretty straightforward, but is nice to be able to easily get and change anything in the 2-d array by col or row and pass them around easily as pointers to other functions.

Cheers,
Tom