gpt4 book ai didi

java - 移动数组中的元素

转载 作者:行者123 更新时间:2023-12-01 12:15:33 27 4
gpt4 key购买 nike

我试图将二维数组中的错误元素移动到元素中的不同空白

'X*''O'' ''X''O*'
'O''O'' '' ''X'
'O'' ''X''X''O*'
'X'' '' ''X''X'
'X''X''O*''X''O*'

标有*的元素为假元素。我想把那些假细胞移动成这样。换句话说,将所有错误元素移动到数组中的空白处

' ''O''X''X'' '
'O''O''O'' ''X'
'O''O''X''X'' '
'X''O''O''X''X'
'X''X'' ''X'' '

这是我到目前为止所得到的,但没有得到结果,它只移动一个元素而不是所有错误元素。当我尝试在外循环中放置中断时,它不起作用。请提供任何提示,这是迄今为止我的代码:

char temp = ' ';
char[][] arr = new char[tissue.length][tissue[0].length];
for (int i = 0; i < tissue.length; i++)
for (int j = 0; j < tissue[i].length; j++){
if (isSatisfied(tissue, i, j, threshold)
arr[i][j] = tissue[i][j];
if ( !isSatisfied(tissue, i, j, threshold)) {
temp = tissue[i][j];
breakloop:
for (int k = 0; k < tissue.length; k++)
for (int l = 0; l < tissue[k].length; l++)
if (tissue[k][l] == ' ') {
tissue[i][j] = arr[k][l];
arr[k][l] = temp;
break breakloop;
}
}
}

最佳答案

这是一个可行的解决方案。处理是通过单个数组完成的,因为它更容易处理。

public class MoveArrayValues
{
final static String F = "X*", E = "O*";
final static String X = "X", O = "O";
final static String B = " ", T = "-";

final static int COLUMN_COUNT = 5;

public static void main( String[] args )
{
String[] source =
{
F, O, B, X, E,
O, O, B, B, X,
O, B, X, X, E,
X, B, B, X, X,
X, X, E, X, E
};

// Print the original source
for ( int ptr = 0; ptr < source.length; ptr++ )
{
System.out.print("[" + source[ptr] + "]");
if ( ptr != 0 && ((ptr + 1) % COLUMN_COUNT) == 0 ) System.out.println();
}

// Initialise the target array and then process the source
String[] target = new String[source.length];
for ( int ptr = 0; ptr < source.length; ptr++ )
{
String cell = source[ptr];
if ( cell.equals(T) )
{ // Skip cells marked as "taken"
continue;
}
if ( cell.equals(F) || cell.equals(E) )
{ // False values
target[ptr] = B; // false value becomes a blank

// now find a new location for this false value
int offset = 1;
/*
* This while loop will find the closest free cell that
* hasn't already been taken (preferring the cell to the
* right).
*
* The while condition is just to make sure we don't
* fall into an endless loop
*/
while ( offset < source.length )
{
if ( ptr + offset < source.length )
{ // Scan to the right
String tmp = source[ptr + offset];
if ( tmp.equals(B) )
{ // found a blank, now use this space
source[ptr + offset] = T; // Mark the space as "taken"
target[ptr + offset] = cell.equals(F) ? X : O;
break;
}
}
if ( ptr - offset >= 0 )
{ // Scan to the left
String tmp = source[ptr - offset];
if ( tmp.equals(B) )
{ // found a blank, now use this space
source[ptr - offset] = T; // Mark the space as "taken"
target[ptr - offset] = cell.equals(F) ? X : O;
break;
}
}
offset++;
}
}
else
{ // Normal values and spaces
target[ptr] = cell;
}
}

System.out.println("--------------------");
// Print the resultant target
for ( int ptr = 0; ptr < target.length; ptr++ )
{
System.out.print("[" + target[ptr] + "]");
if ( ptr != 0 && ((ptr + 1) % COLUMN_COUNT) == 0 ) System.out.println();
}
}
}

如果您确实需要使用二维数组,那么您可以修改外部 for 循环和内部 while 循环,或者更简单,编写一个小方法来转换将二维数组转换为单个数组,这非常容易。

关于java - 移动数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com