简体   繁体   English

如何从Java中的(5x5)2d数组中删除空白,从而移动数组中的值?

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

I have a 5x5 array and I am trying to 1) remove characters based on characters entered by user's String (accomplished) and 2) shift the array values to the right, thus opening up array space at the front for the user input String (not accomplished). 我有一个5x5的数组,我试图1)根据用户的String(完成的)输入的字符删除字符,2)将数组的值向右移动,从而为用户输入的String(不是完成)。

Right now, if I type in "Jake" the user input is: 现在,如果我输入“ Jake”,则用户输入为:

 bcd
fgh
lmnop
qrstu
vwxyz

(this is because 'j' 'a' 'k' and 'e' have been removed .. please ignore the absence of 'i' because the assignment uses 'i' and 'j' as the same character in order to squeeze into a 5x5 array) (这是因为'j''a''k'和'e'已被删除..请忽略'i'的缺失,因为赋值使用'i'和'j'作为相同字符以便挤压到5x5阵列)

I want 'b' to be at the end of the first row (and at grid[0][4]) so that I can fit "Jake" into the beginning. 我希望“ b”位于第一行的末尾(并位于grid [0] [4]),以便我可以将“ Jake”放到开头。 Please let me know how to modify my code to make this work, thus "trimming" the graph down and to the right, if you will. 请让我知道如何修改我的代码以完成此工作,如果可以的话,可以从右下方“修剪”图形。 Any help is appreciated!! 任何帮助表示赞赏!

` `

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] = '\0';
        }
        else
        {
            grid[row][col] = '\0';  
        }
    }   
}

` `

To achieve that, you can traverse the matrix from the last element ie @ [5][5] If you find empty character, push the first non-empty character you find forward to fill the gap. 为此,您可以从最后一个元素(即@ [5] [5])遍历矩阵。如果找到空字符,请向前推找到的第一个非空字符以填补空白。 For example, start at z 例如,从z开始

 bcd
fgh
lmnop
qrstu
vwxyz

You will find first non-empty at [2][5] , so you push h to [2][5]. 您将在[2] [5]处找到第一个非空对象,因此将h推至[2] [5]。 You get empty at [2][4] again, so you push g to [2][4]. 您再次在[2] [4]处为空,因此将g推至[2] [4]。 Keep doing this till you reach [1][1]. 继续这样做,直到达到[1] [1]。 (assume indexing starts from [1][1]) Hope that helps. (假设索引从[1] [1]开始)希望有帮助。

It is much easier if you represent the matrix as a String . 如果将矩阵表示为String ,则容易得多。 Then you can use .replaceAll to remove the chars in the key: 然后,您可以使用.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();
    }
}

First, I recommend to take the line grid[row][col] = ch; 首先,我建议采用网格线[row] [col] = ch; out of the if/else block, as it appears twice: 在if / else块之外,出现两次:

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++;
    }
  }
}

Next, have a method to right shift characters in a 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 中为 5x5 二维数组正确使用制表符空间? - How to properly use tab spaces in Java for a 5x5 2D array? Java中的垂直5x5 2D阵列 - Vertical 5x5 2D Array in Java 如何创建 5x5 二维数组的 2x2 子数组,旋转它,然后将其添加回原始数组? - How do I create a 2x2 subarray of a 5x5 2d array, rotate it, then add it back to the original array? Java - 文本到二维数组删除空格” - Java - text to 2d array remove spaces" 我怎样才能更好地实现使用 2D 阵列激活网格上周围按钮的 5x5 网格按钮拼图? - How can I better achieve my implementation of a 5x5 grid puzzle of buttons that activate the surrounding buttons on the grid using a 2D Array? Java:如何从二维双精度数组中获取指定坐标的整数(x,y) - Java: How do I get the integer (x,y) of a specify coordinate from a 2d double array 如何从Java中的2D数组中删除特定行和特定列? - How do I remove a specific row and a specific column from a 2D Array in Java? 在2D数组中移动元素 - Java - Shifting elements in a 2D array - Java Java上5x5的盒子阵列? - 5x5 array of boxes on Java? 在Java中,如何获取2D数组的值并将它们存储在具有不同列和行大小的另一个2D数组中? - In Java, how do I take the values of a 2D array and store them in another 2D array with different column and row size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM