Zurück   Flashforum > Flash > ActionScript > ActionScript 1

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 18-05-2006, 18:01   #1 (permalink)
Coyote79
Gast
 
Beiträge: n/a
array richtig kopieren ?!?

Hi @ all ...

hab mich mal im Forum umgeschaut, aber nichts passendes gefunden. Vielleicht kann mir jemand helfen.

Ich habe das Problem, dass ich ein Array erstelle, eine Kopie davon anlege und Änderungen in der Kopie auch Auswirkungen auf das Original-Array haben.

Hier mal der Code:

Code:
matrix = createMatrix();

changeMatrix();


function createMatrix():Array {
	
	var matrix:Array = new Array;

	for (var i:Number = 0; i < 9; i++){
		matrix[i] = new Array;
	}

	matrix[0] = new Array(2,4,7,5,9,3,6,8,1)
	matrix[1] = new Array(8,6,1,2,4,7,5,9,3)
	matrix[2] = new Array(5,3,9,6,8,1,2,7,4)
	matrix[3] = new Array(7,8,3,4,1,5,9,6,2)
	matrix[4] = new Array(4,2,6,8,7,9,1,3,5)
	matrix[5] = new Array(1,9,5,3,6,2,8,4,7)
	matrix[6] = new Array(9,1,4,7,5,8,3,2,6)
	matrix[7] = new Array(3,7,8,1,2,6,4,5,9)
	matrix[8] = new Array(6,5,2,9,3,4,7,1,8)
	
	return (matrix);
}

function changeMatrix() {

	var newmatrix:Array = new Array;  
	
	for (var i:Number = 0; i < 9; i++){
		newmatrix[i] = new Array;
		newmatrix[i] = matrix[i];
	}
	
	//Wieso werden hier 2 unterschiedliche Werte für matrix ausgegeben, obwohl ich newmatrix ändere ???
	trace (matrix[0][0]);
	newmatrix[0][0] = 0;
	trace (matrix[0][0]);
	
}

Geändert von Coyote79 (18-05-2006 um 18:16 Uhr)
  Mit Zitat antworten
Alt 18-05-2006, 18:22   #2 (permalink)
voidboy
 
Benutzerbild von rendner[i]
 
Registriert seit: Sep 2004
Ort: München
Beiträge: 5.588
Du kopierst ja nicht das Array sondern speicherst eine Referenz auf dieses in der neuen Variable.
PHP-Code:
newmatrix[i] = new Array();
newmatrix[i] = matrix[i]; 
Wenn das zu kopierende Array 1 dimensional ist kann man es mit folgendem Trick kopieren.
PHP-Code:
newmatrix[i] = new Array();
newmatrix[i] = [].concatmatrix[i] ); 
Wie gesagt geht nur wenn du ein Array kopierst was 1 dimensional ist, das hier geht nämlich schon nicht mehr:
PHP-Code:
function changeMatrix() {

    var 
newmatrix:Array = [].concatmatrix );  

    
trace (matrix[0][0]);
    
newmatrix[0][0] = 0;
    
trace (matrix[0][0]);

Ach ja die Konstruktoren in Flash sollten mit einem () am ende aufgerufen werden.
Also new Array() und nicht new Array.
__________________
ERROR: Signature is too large
rendner[i] ist offline   Mit Zitat antworten
Alt 18-05-2006, 22:39   #3 (permalink)
Techniker
 
Benutzerbild von hgseib
 
Registriert seit: Sep 2003
Ort: 64807
Beiträge: 16.321
http://www.seibsprogrammladen.de/fra...orithmen/Array
Array duplizieren
__________________
die ultimative antwort auf alle programmierfragen: der debugger
mfg h.g.seib www.SeibsProgrammLaden.de
hgseib ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 11:43 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele