The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Hello!

First I have to say that I really like Groovy and the great book "Groovy in Action".
Second, the following problem isn't a certain one from the book. But I hope you can give me some advice anyway.

I tried to write a Groovy-script that solves sudokus. (I won't post the whole code in order to avoid flooding this thread.)
In the context of this work I wanted to store some lists.
In the subsequent lines of the script I manipulated these lists. And the saved copies of the lists got manipulated too!

Here some lines of my script:

class TemporaryClass {
private temporaryfreeFields = [:]
private temporarySudoku = [:]

def addFieldsList(list, j) {
temporaryfreeFields[j] = list
}

def addSudokuList(list, j) {
temporarySudoku[j] = list
}
}

def temporaryObjects = new TemporaryClass()

def h = 0

def suche(temporaryObjects, freieFields, sudoku, h) {
// Aktuelle Liste der unbesetzten Felder speichern
temporaryObjects.addFieldsList(freieFields, h)

// Umkopieren, sonst wirken Veränderungen zurück auf die Speicherung in der Klasse.
def newFreeFields = []
newFreeFields.addAll(freieFields)

// Feld aus der Liste der unbesetzten Felder holen
def feld = newFreeFields.pop()

println newFreeFields.size()
println temporaryObjects.temporaryfreeFields[0].size()

// Umkopieren, sonst wirken Veränderungen zurück auf die Speicherung in der Klasse.
def newSudoku = []
newSudoku.addAll(sudoku)


// Aktuelles Sudoku speichern
temporaryObjects.addSudokuList(sudoku, h)

println newSudoku.flatten().size()
println temporaryObjects.temporarySudoku[0].flatten().size()

// Schleife über alle freien Zahlen des gewählten Feldes
// Die freien Zahlen des gewählten Feldes in einer Hilfsvariable zwischenspeichern
def fieldList = newSudoku[feld[0]][feld[1]]
for (j in 0..fieldList.size()-1) {
// Feld mit beliebigem freien Wert besetzen
// These changes also appear in the stored list of the class!
newSudoku[feld[0]][feld[1]] = fieldList.pop().toList()

}
println newSudoku.flatten().size()
}

suche(temporaryObjects, freeFields, setup, 0)


Most likely I made a terrible mistake. But I am a bit concerned. Thanks for helping me.

Greets!

Elmar