I've been battling with this for some time and seem to be getting nowhere. The set up is so; I have a 2D array. For this array I need to iterate through each value and return the diagonal neighbours (5 values). These neighbours will be put into a new 1D [5] array and bubblesorted. The middle value (median) will then be returned and put into a new array of medians.
So far I have methods for extracting the diagonal neighbours:
//get diagonals from original DEM
double [] getDiagonals(int i, int j) {
double [] tempArray = new double [5];
tempArray[0] = data[i -1][j +1];
tempArray[1] = data[i -1][j -1];
tempArray[2] = data[i][j];
tempArray[3] = data[i +1][j -1];
tempArray[4] = data[i +1][j +1];
return tempArray;
}
I've then used this method in an iteration to get diagonals for each value in the original array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
//do something with the tempArray
I think this is where I'm coming unstuck. Through testing the getDiagonals method works fine. I'm struggling to get the tempArray out of the bubbles() method. If I set the output to be the tempArray it only returns the 5 values calculated for the bottom right corner of the original array.
I've tried calling other methods into the bubbles() method in order to do all the processing there and return a new array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
double sorted [] = sort(tempArray);
double median = sorted[2];
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
}
}
}
}
return datap;
}
Again this fails and the output datap is just zeros. The sort() method above passed out the diagonals to a bubble sort method (which I know works on its
I guess my question is how to process within a method that is iterating and populate a new array?
I hope this makes sense but if you need more details please let me know. And yes, the sort I'm using is a bubble sort. I know they are rubbish but this is for a course I'm doing so it has to be used. And yes, I'm pretty new to java.
Any help would be greatly appreciated (and I'll even reference you if I need to use some code you provide ;)
Finally cracked it. To populate the whole array the following code works a peach.
The sorting was taken out and I haven't tested with it back in yet; but so far this is providing new values for all the cells in the temp array.