我有一个Postgres数据库。我的一个表中的一列包含数组。它被称为public override Move GetMove(Board board)
{
List<Move> moves = board.getMoveList();
double bestValue, currentValue; ;
Move bestMove = moves.First();
Board firstBoard = board.DeepClone();
firstBoard.Move(bestMove);
bestValue = Evaluator(firstBoard);
List<Move> tieMoves = new List<Move>();
foreach (Move aMove in moves)
{
Board board2 = board.DeepClone();
board2.Move(aMove);
// If there is a move in one take it and leave
if (board2.WhoWon() == side)
return aMove;
// check player 2 moves
foreach (Move bMove in moves)
{
Board board3 = board2.DeepClone();
board3.Move(bMove);
// If there is a move in one take it and leave
if (board3.WhoWon() == side)
return aMove;
// check player 1 future moves
foreach (Move bMove in moves)
{
Board board4 = board3.DeepClone();
board4.Move(cMove);
// If there is a move in one take it and leave
if (board4.WhoWon() == side)
return aMove;
currentValue = Evaluator(board4);
if (
(side == Pieces.ATTACK && currentValue > bestValue)
|| (side == Pieces.DEFEND && currentValue < bestValue)
)
{
tieMoves = new List<Move>();
tieMoves.Add(aMove);
bestMove = aMove;
bestValue = currentValue;
}
}
}
}
// Pick one from the candidate moves with the same value
return tieMoves.ElementAt(random.Next(tieMoves.Count));
}
。我正在尝试合并两个实例,但是当我尝试合并数组时,它们没有保存。
import static java.lang.System.*;
import static javax.swing.JOptionPane.*;
import static java.lang.Integer.*;
public class SimpleCalc {
public static void main(String[] args) {
String operator = showInputDialog("Choose operation: " + "\n" +
"[1] = Plus" + "\n" +
"[2] = Minus" + "\n" +
"[3] = Multiply" + "\n" +
"[4] = Divide" + "\n");
int c = parseInt(operator);
if (c > 4) {
showMessageDialog(null, "You cant do that.");
} else if (c == 1) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " + " + b + " = " + (a + b));
} else if (c == 2) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " - " + b + " = " + (a - b));
} else if (c == 3) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " * " + b + " = " + (a * b));
} else if (c == 4) {
String textA = showInputDialog("Enter first number: ");
String textB = showInputDialog("Enter second number: ");
int a = parseInt(textA);
int b = parseInt(textB);
showMessageDialog(null, a + " / " + b + " = " + (a / b));
}
}
}
但是当我检查aliases
时,别名没有合并。
irb(main):001:0> original
=> #<Thing id: 1, name: "Foo", aliases: ["Foo"]>
irb(main):002:0> duplicate
=> #<Thing id: 2, name: "Bar", aliases: ["Bar"]>
irb(main):003:0> original.aliases | duplicate.aliases
=> ["Foo", "Bar"]
irb(main):004:0> original.save!
(0.3ms) BEGIN
Thing Exists (0.8ms) SELECT 1 AS one FROM "thing" WHERE "things"."name" = $1 AND ("thing"."id" != $2) LIMIT $3 [["name", "Foo"], ["id", 1], ["LIMIT", 1]]
(0.3ms) COMMIT
Thing Store (6.1ms) {"id":1}
=> true
我尝试了几种不同的方式将original
插入irb(main):005:0> original
=> #<Thing id: 1, name: "Foo", aliases: ["Foo"]>
,但似乎没有任何保存。有什么想法吗?我错过了什么吗?
编辑: 我在Rails 5.1.4和Ruby 2.5.0p0
上答案 0 :(得分:2)
你是否在Rails 3上?我注意到Rails3 activerecord数组处理不喜欢内联更新。您需要使用临时变量,扩充该变量,然后将其分配回来。
aliases = original.aliases # make a temp variable
aliases = aliases | duplicate.aliases # modify temp variable
original.aliases = aliases # assign back
original.save! # now you can save
当我试图推进阵列时,我自己遇到了这个问题:
original.aliases << "something" # aliases would never get changed