Java shifting a 2D array to the right, with last column being placed in first

biglemon29 :

Hi I was wondering if I could get help creating a method to shift a java 2D array to the right

public void filter(PixelImage pi) {
    Pixel[][] data = pi.getData();  // get image data

    for (int row = 0; row < pi.getHeight(); row++) {
        for (int col = 0; col <  pi.getWidth(); col++) {
               if(col == pi.getWidth()-1){
                   Pixel lastCol = data[row][pi.getWidth()-1];
                   data[row][0] = lastCol;
                }
               else{    
                Pixel temp1 = data[row][col];              
                data[row][col+1] = temp1;
            }
        }
    }  
    // reset data into the PixelImage object pi
    pi.setData(data);
}

Here is my code sample that is not working properly, could someone please point me in the right direction?

Mohammad Zohrabi :

this might be work.

public void filter(PixelImage pi) {
    Pixel[][] data = pi.getData();  // get image data

    for (int row = 0; row < pi.getHeight(); row++) {
        Pixel lastCol = data[row][pi.getWidth()-1];
        for (int col = pi.getWidth() - 1; col > 0; col--) {          
                data[row][col] = data[row][col-1];
            }
        data[row][0] = lastCol;
    }  
    // reset data into the PixelImage object pi
    pi.setData(data);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396657&siteId=1