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.
 
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.
 
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);
					}
 
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 ^^.
 
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:
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:
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));
 
aye, i had already imported it - was the first thing i tried.

and have altered the calls, same error.
 
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