[英]Moving elements in array
我正在嘗試將2d數組中的false元素移動到element中的另一個空白空間
'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();
}
}
}
如果確實需要使用2d數組,則可以修改外部for
循環和內部while
循環,或者甚至更簡單,編寫一些方法將2d數組轉換為單個數組,這非常容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.