简体   繁体   中英

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).

Right now, if I type in "Jake" the user input is:

 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)

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. 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. For example, start at z

 bcd
fgh
lmnop
qrstu
vwxyz

You will find first non-empty at [2][5] , so you push h to [2][5]. You get empty at [2][4] again, so you push g to [2][4]. Keep doing this till you reach [1][1]. (assume indexing starts from [1][1]) Hope that helps.

It is much easier if you represent the matrix as a String . Then you can use .replaceAll to remove the chars in the key:

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; out of the if/else block, as it appears twice:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM