翻转的多维阵列的java [英] flipping a multi-dimensional array java

查看:119
本文介绍了翻转的多维阵列的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有创建字符的具有所需宽度和高度的画布类

i have a class that creates a canvas of characters with a desired width and height.

在此我有一个方法drawLetter()(这基本上改变了角色,这样的信出现在画布上(比如很简单的ASCII艺术)

within this i have a method drawLetter() (which basically changes the characters so that a letter appears on the canvas (like very simple ASCII art)

我还有其他3种方法rotateClockwise,flipHorizo​​ntal和flipVertical

i have 3 other methods rotateClockwise, flipHorizontal, and flipVertical

旋转和flipHorizo​​ntal我这样做很好,他们的工作,但我似乎无法制定出在哪里IM与垂直翻转阵列脚麻(出于兴趣我想我有横向和纵向的南辕北辙,但生病把样本什么即时试图让下面执行)

rotate and flipHorizontal i have done fine and they work but i cannot seem to work out where im going wrong with flipping the array vertically (out of interest i think i have horizontal and vertical the wrong way round but ill put a sample of what im trying to get done below)

这就是我不能工作了:

这样的:

# # # ~
~ # ~ ~
~ # ~ ~
~ ~ ~ ~

这样:

~ # # #
~ ~ # ~
~ ~ # ~
~ ~ ~ ~

显然这将在完成循环就像我的其他方法,下面我会后我已经做了横向的方法和顺时针方向旋转,以证明它不是功课我不能做

obviously it will be done in a for loop like my other methods, below i will post the method i have done already for horizontal and rotate clockwise to prove its not homework i cant do

void mirrorHorizontally()
{
    char[][]horizontalImage = new char[height][width];

    for (int i = 0; i < height /2; i++)
    { 
       for(int j = 0; j < width; j++)
       {  
           horizontalImage[height - (i+1)][j] = canvasArray[i][j];           
           horizontalImage[i][j] = canvasArray[height - (i+1)][j];
       }
    }   
    printPicture(horizontalImage);
}


void rotateClockwise()
{
    char[][] rotatedImage = new char[height][width];

    for(int i=0; i< canvasArray.length; i++)
    {
        for(int j= canvasArray.length-1; j >= 0; j--)
        {
            rotatedImage[i][rotatedImage.length-1-j] = canvasArray[j][i]; 
        }
    }

    printPicture(rotatedImage);
}

canvasArray是一个char [] []原始图像出于兴趣类变量

canvasArray is the original image out of interest in a char[][] class variable

推荐答案

这应该满足你的需要:

public static char[][] mirror(int width, int height, char[][] in) {
    char[][] out = new char[height][width];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            out[i][width - j - 1] = in[i][j];
        }
    }
    return out;
}

例如:

char[][] in = new char[][] {
    new char[] { '#', '#', '#', '~' },
    new char[] { '~', '#', '~', '~' },
    new char[] { '~', '#', '~', '~' },
    new char[] { '~', '~', '~', '~' },
    new char[] { '~', '~', '~', '~' }
};

for (char[] line : mirror(4, 5, in)) {
    for (char row : line) {
        System.out.print(row);
    }
    System.out.println();
}

打印:

~###
~~#~
~~#~
~~~~
~~~~

这篇关于翻转的多维阵列的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆