matrix transposistion in java

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
6,981
Location
Manchester
hi.

wondering if anyone can offer me some advice..

i have an array of characters which is [2][3], with the word hello! in it. visually like so

Code:
H E L
L O !

i want to transpose this matrix and read it back into a string (HLEOL!), but i'm not sure how to approach this, my java is rusty and i've found many examples but they seem to deal with matrices of integers and get upset when i try and use chars.

greatful of any help you guys can offer.
 
Code:
    public char[][] transpose(char[][] array) {
        // Create a tempory array with the transposed dimensions
        char[][] temp = new char[array[0].length][array.length];
        // Iterate over the exiting array (parameter)
        for (int i = 0; i < array.length; i++) 
            for (int j = 0; j < array[0].length; j++)
                // Populate the new array with the exiting array data
                // the data is inserted transposed
                temp[j][i] = array[i][j];
        // Return the new array
        return temp;
    }

Input: [[H, E, L], [L, O, !]]
Output: [[H, L], [E, O], [L, !]]

Here is a method to turn a 2D character array into a string:

Code:
    public String multiCharToString(char[][] array) {
        // Create a tempory array to hold all the elements
        char[] temp = new char[array[0].length*array.length];
        // Pointer to iterate through new array
        int pointer = 0;
        // Iterate over 2D array
        for (char[] chs : array) {
            for (char c : chs) {
                // Add element to new array
                temp[pointer] = c;
                // Increment the pointer
                pointer++;
            }
        }
        // Return the character array as a String
        return new String(temp);
    }

Input: [[H, L], [E, O], [L, !]]
Output: HLEOL!
 
Last edited:
that is very helpful, thank-you.

when compiling the second one, i am getting an error from the line

for (char c : chs) {

it does not like the : and says ; expected.
 
Are you using java 1.4 or earlier? The foreach statement is only available from 1.5. Before then you need to use for loops.

I've modified Rob's implementation for this and also made it slightly simpler by replacing the temporary array with a StringBuffer.

Code:
    public static String multiCharToString(char[][] array) {
        // temporary object to hold our string
        StringBuffer sb = new StringBuffer();
        
        // Iterate over 2D array and build a string of its
        // contents.
        for (int i=0; i<array.length; i++) {
            for (int j=0; j<array[0].length;j++) {
            	sb.append(array[i][j]);
            }
        }
        
        return sb.toString();
    }
 
Last edited:
I've modified Rob's implementation for this and also made it slightly simpler by replacing the temporary array with a StringBuffer.

I've just run some simple execution timing tests (100, 1000, 10000, 1000000 cycles) on both methods to see how efficient they are, mine comes out ever so slightly more efficient because I'm dealing with the raw array and not a StringBuffer. I do agree that your method is slightly easier to read then mine though, it depends on what you want.
 
Method 1 is your method using the StringBuffer and Method 2 is mine using the tempory array.

100 iterations

Method 1 Average: 5735 ns
Method 2 Average: 3927 ns

1000 iterations

Method 1 Average: 6078 ns
Method 2 Average: 3951 ns

10000 iterations

Method 1 Average: 5323 ns
Method 2 Average: 3216 ns

1000000 iterations

Method 1 Average: 2002 ns
Method 2 Average: 1838 ns
 
thanks for the help there guys, one last thing - i am wanting to convert a string into a 2D array. i can define the array size based on message length, but am not the best method to populate the array. should i be looking at string tokenizer.

this is what i have for creating the array sizes

Code:
if (messageLength % 2 == 0) { // checking if message length is even
	        			arrayX = 2;
	        			arrayY = (messageLength / 2);
					}

if (messageLength % 2 != 0) { // message length not even, rounds down and then ensure arrayY is big enough
					arrayX = 2;
					arrayY = Math.round((messageLength / 2) +1);
					}
 
thanks for the help there guys, one last thing - i am wanting to convert a string into a 2D array. i can define the array size based on message length, but am not the best method to populate the array. should i be looking at string tokenizer.

Can you give an example string and an example array? How do you know what dimensions you want the array?
 
the string is just random text, i am using the transposistion to do a basic level of encrpytion on it. for that, i can know that one dimension of the array can 2, and work out the other dimension based on that and the length of the string ^^ as above ^^.
 
What do you do when you have an odd length string, do you place anything the gap at the end?

EG:

[ [H, e, l, l, o, !], [W, o, r, l, d, <gap here>] ]
 
if the length is odd, it is divided by 2, rounded down and then has 1 added to it. This means there will be a max of 1 spare space on the end of the array, which is fine...

..actually, if it then tried to read that it would see null and not a space wouldnt it.. so i need to fill the array with spaces before the data goes in i think, or at least bung a space on what would be a spare location.

edit, this'd do it

Code:
if (messageLength % 2 != 0) {
					messageArray[1][(arrayY - 1)] = ' ';
					}
 
Last edited:
I played around with for loops but I kept getting ArrayIndexOutOfBounds exceptions because the loop had nothing to insert into the last space of the second array if the string was odd. I found some useful array operations and it's much easier now and works perfectly.

Code:
    public char[][] stringTo2dArray(String str) {
        // Message characters
        char[] strArray = str.toCharArray();
        // Message length
        int size = strArray.length;
        // Half message length
        int half = (size/2)+(size%2);
        // Initialise new array to correct dimensions
        char[][] newArray = new char[2][half];
        // Copy first half of message to new array
        newArray[0] = java.util.Arrays.copyOfRange(strArray,0,half);
        // Copy second half of message to new array & pad if odd length string
        newArray[1] = java.util.Arrays.copyOfRange(strArray,half,size+(size%2));
        // Return the new array
        return newArray;
    }

Here are some sample calls and the results:

Code:
stringTo2dArray("HelloWorld");

[[H, e, l, l, o], [W, o, r, l, d]]

stringTo2dArray("Hello!World");

[[H, e, l, l, o, !], [W, o, r, l, d,  ]]

Note: The second call has a null character as the last element of the array.
 
where abouts did you find them useful array operations, i don't seem to have them! i just updated to latest version, and still not there!

edit; my bad.

it isnt liking what is being passed here

newArray[0] = java.util.Arrays.copyOfRange(strArray,0,half);

its expecting char[],int,int

or am i being dumb here. which is possible, it's getting quite late!
 
Last edited:
where abouts did you find them useful array operations, i don't seem to have them! i just updated to latest version, and still not there!

edit; my bad.

it isnt liking what is being passed here

newArray[0] = java.util.Arrays.copyOfRange(strArray,0,half);

its expecting char[],int,int

or am i being dumb here. which is possible, it's getting quite late!

Make sure you copied it correctly, it should work as strArray is of type char[], 0 is an int and so is half. If you changed any of the variable names then that would cause a problem. If you haven't fixed it tomorrow post the code and I'll take a look.
 
copied it straight into textpad from here, error message;

Code:
tp.java:15: cannot resolve symbol
symbol  : method copyOfRange (char[],int,int)
location: class java.util.Arrays
        newArray[0] = java.util.Arrays.copyOfRange(strArray,0,0);

and same error the line;

newArray[1] = java.util.Arrays.copyOfRange(strArray,half,size+(size%2));
 
EDIT: Make sure your using java 1.6 (JAVA 6) I have just seen that these methods are only present in Java 1.6 and above.
 
Last edited:
aye, i had already imported it - was the first thing i tried.

and have altered the calls, same error.
 
aye, i had already imported it - was the first thing i tried.

and have altered the calls, same error.

See above, you need to install Java 6 SDK and make sure your IDE's are using it to compile your project with. Only just relised that they were added in Java 6, probably why I didn't know they existed. They is probably a way to do it that is Java 5 compatible though but I can't think of it right now.

Just a note, Java 6 = Java 1.6
 
sorted now,i had installed 1.6 but not removed the references to 1.4 in environment variables.

i think that is a sign i should go to bed, thank-you very much for your help. If you ever need any car related assistance, give me a shout :)
 
Back
Top Bottom