如何从 java 中的 (5x5) 二维数组中删除空格,从而移动数组中的值?

How do I remove empty spaces from a (5x5) 2d array in java, thus shifting values in the array?

我有一个 5x5 数组,我正在尝试 1) 根据用户字符串输入的字符删除字符(已完成)和 2) 将数组值向右移动,从而打开数组 space前面为用户输入的String(未完成)。

现在,如果我输入 "Jake",用户输入是:

 bcd
fgh
lmnop
qrstu
vwxyz

(这是因为 'j' 'a' 'k' 和 'e' 已被删除..请忽略 'i' 的缺失,因为赋值使用 'i' 和 'j' 作为同一个字符,以便挤入一个 5x5 数组)

我希望 'b' 位于第一行的末尾(并且在网格 [0][4] 处),以便我可以将 "Jake" 放入开头。请让我知道如何修改我的代码以使其工作,因此 "trimming" 图形向下和向右,如果你愿意的话。感谢您的帮助!!

`

grid = new char[5][5];
char ch = 'a'; 
for (row = 0; row < 5;row++) 
{
  for (col = 0; col < 5; col++)
  {
    if(ch=='i') // if we are at i
    {
        grid[row][col] = ch;    // add i to grid
        ch+=2;  // increment ch twice (k)
    }
    else    // if we are not dealing with i/j
    {
        grid[row][col] = ch;    // add the char
        ch++;
    }
  }
}
for(row = 0; row < 5; row++)
{
  for(col = 0; col < 5; col++)
  {
    if(key.indexOf(grid[row][col]) >= 0 || (key.indexOf('j') >= 0 && grid[row][col] == 'i'))
    {   
        if(grid[row][col] == 'i' && key.indexOf('j') >= 0) 
        {
            grid[row][col] = '[=12=]';
        }
        else
        {
            grid[row][col] = '[=12=]';  
        }
    }   
}

`

为此,您可以从最后一个元素开始遍历矩阵,即@[5][5] 如果找到空字符,将找到的第一个非空字符向前推以填补空白。 例如,从 z

开始
 bcd
fgh
lmnop
qrstu
vwxyz

你会发现在 [2][5] 处第一个非空,所以你将 h 推到 [2][5]。你在 [2][4] 处又变空了,所以你将 g 推到 [2][4]。继续这样做,直到到达 [1][1]。 (假设索引从 [1][1] 开始) 希望对您有所帮助。

如果将矩阵表示为 String 会容易得多。然后你可以使用 .replaceAll 删除键中的字符:

public static void main(String[] args) {
    String key = "Jake";

    // Build string
    String str = "";
    char ch = 'a';
    for (int c = 0; c < 25; c++) {
        str += ch;
        ch++;
        if (ch == 'i') {
            ch++;
        }
    }
    System.out.println(str);

    // Remove chars from key
    String modifiedKey = key.toLowerCase().replace('i', 'j');
    for (int i = 0; i < key.length(); i++) {
        str = str.replaceAll("" + modifiedKey.charAt(i), "");
    }
    System.out.println(str);

    // Add key in the beginning
    str = key.toLowerCase() + str;
    System.out.println(str);

    // Print out
    for (int row = 0; row < 5; row++) {
        for (int col = 0; col < 5; col++) {
            System.out.print(str.charAt(row * 5 + col));
        }
        System.out.println();
    }
}

首先,我建议从 if/else 块中取出 grid[row][col] = ch; 行,因为它出现了两次:

grid = new char[5][5];
char ch = 'a'; 
for (row = 0; row < 5;row++) 
{
  for (col = 0; col < 5; col++)
  {
    grid[row][col] = ch;    // add i to grid

    if(ch=='i') // if we are at i
    {
        ch+=2;  // increment ch twice (k)
    }
    else    // if we are not dealing with i/j
    {
        ch++;
    }
  }
}

接下来,有一个方法可以右移table中的字符:

private char[][] rightShift(char[][] table) {
  int rows = table.length;
  int columns = table[0].length;

  char[][] result = new char[rows][columns];
  int outputIndex = rows*columns - 1;

  for (int inputIndex = rows*columns - 1; inputIndex <= 0; inputIndex--) {
    int inputRow = inputIndex / rows;
    int inputColumn = inputIndex % columns;

    int outputRow = outputIndex / rows;
    int outputColumn = outputIndex % columns;

    if (table[inputRow][inputColumn] != ' ') {
      result[outputRow][outputColumn] = table[inputRow][inputColumn];
      outputIndex--;
    }
  }

  for (int i = 0 ; i < outputIndex - inputIndex; i++) {
    int outputRow = i / rows;
    int outputColumn = i % columns;
    result[outputRow][outputColumn] = ' ';
  }

  return result;
}