Array Problem

Hello,

I am a bit confused on why this code is not working. I have a two-dimensional array where I store String elements. I call it String [ ][ ] DixElements. Then I have another one-dimensional array, String [ ]backArray, where I copy-backwards each row of the two-dimension arraystarting from a row before the last Array.

public String [ ] BStorage(){

int size = DixElements.length;//two-dimensional array (globally defined)

for(int b = size ; b > 0; b-- ){

backArray =new String [DixElements[b-1].length];//backArray is a one-dimensional array and it is globally defined

for (int d = 0; d < backArray.length; d++){

backArray[d] = DixElements[b-1][d];

System.out.println(backArray[d]);

}

}

return backArray;

}

If I run it, I always get all the rows except the first row but I wanted to get all the rows except the last row.

Thanks for the help.

Jona_T

[1453 byte] By [Jona_Ta] at [2007-11-27 3:59:02]
# 1

Have you tried

for (int b = size; b > 1; b--)

{

backArray = new String [DixElements[b - 2].length];

for (int d = 0; d < backArray.length; d++)

{

backArray[d] = DixElements[b - 2][d];

}

}

remember that DixElements[size-1] is your last row of the array. To get all but the last row you need to do DixElements[size - 2] to DixElements[0]

Message was edited by:

petes1234

petes1234a at 2007-7-12 9:03:32 > top of Java-index,Java Essentials,New To Java...
# 2
Change:for(int b = size ; b > 0; b-- ) {tofor(int b = size-1 ; b => 0; b-- ) {
abillconsla at 2007-7-12 9:03:32 > top of Java-index,Java Essentials,New To Java...
# 3

Here's a working example of what I meant:

public class Foobar

{

static String[][] DixElements = new String[][]

{

{"this is line 00", "this is line 01", "this is line 02"},

{"this is line 10", "this is line 11", "this is line 12"},

{"this is line 20", "this is line 21", "this is line 22"}

};

static String[][] BStorage(String[][] dixElmnts)

{

String[][] backArray = new String[dixElmnts.length - 1][];

for (int i = 0; i < backArray.length; i++)

{

backArray[i] = new String[dixElmnts[backArray.length - i - 1].length];

for (int j = 0; j < backArray[i].length; j++)

{

backArray[i][j] = dixElmnts[backArray.length - i - 1][j];

}

}

return backArray;

}

public static void main(String[] args)

{

for (int i = 0; i < DixElements.length; i++)

{

for (int j = 0; j < DixElements[i].length; j++)

{

System.out.println(i + ", " + j +": " + DixElements[i][j]);

}

}

System.out.println();

String[][] myResultArr = BStorage(DixElements);

for (int i = 0; i < myResultArr.length; i++)

{

for (int j = 0; j < myResultArr[i].length; j++)

{

System.out.println(i + ", " + j +": " + myResultArr[i][j]);

}

}

}

}

petes1234a at 2007-7-12 9:03:32 > top of Java-index,Java Essentials,New To Java...
# 4
Hello petes1234,That is good of you for the code. I just discovered that my actual problem is the code that makes use of the backArray and not backArray itself.. So all the backArray function that I wrote were just not working. Your duke star is on the way.Jona_T
Jona_Ta at 2007-7-12 9:03:32 > top of Java-index,Java Essentials,New To Java...