Arrayprobleme

Vasquez

Grünschnabel
Hi!

Ich habe folgendes Problem. Ich erstelle ein zweidimensionales Araay der Groess 20 x 20. Jedes Feld wird von einem Cell Objekt, das ich selbste konstruiere , belegt.

Wenn ich meine Methode setValue, die das Attribut "value" EINES Cellobjektes verändern soll, aufrufe, setzt er ALLE ! Werte aller Cellobjekte auf diesen wert. In meinem Beispielcode ist dies 5. Ich schmeiss den Code nochmal mit rein... hoffe hier weiss jemand was.

Vielen dank schonmal im Voraus!
Code:
--------------------------------------------------
package game;

public class Cell
{
    private int value = 0;
    private boolean visited = false;
    
    
    public Cell()
    {
	value = 0;
	visited = false;
    }
    
    public int getValue()
    {
	return(value);
    }

    public void setValue(int k)
    {
	Cell theCell = this;
	value = k;
    }

}

-----------------------------------------------

package game;

public class Feld
{
    private Cell[][] meinFeld = new Cell[20][20];
    private Cell theCell = null;
    
    public Feld()
    {
	
	theCell = new Cell();
	for( int i=0; i<=19; i++)
	    {
		for( int j=0; j<=19; j++)
		    {
			meinFeld[i][j] = theCell;
			
		    }
	    }
    }

    public Cell[][] getFeld()
    {
	return(meinFeld);
    }

    public void drucke()
    {

	for(int i=0; i<=19; i++)
	    {
		System.out.print("\n");
		for(int j=0; j<=19; j++)
		    {
			System.out.print(meinFeld[i][j].getValue() + " ");
		    }
	    }
    }

    public Cell getCell( int x , int y)
    {
	Feld theFeld = this;
	return( theFeld.getFeld()[x][y]);
    }
	
}

----------------------------------------------------------

package game;


public class Test
{
    
    Feld theFeld = null;
    
    public static void main( String [] args)
    {
	Feld theFeld = new Feld();
	theFeld.drucke();
	theFeld.getCell( 1, 1).setValue( 5);
	theFeld.drucke();
    }
}

----------------------------------------

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0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5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
Process game.Test finished

Das ist die Ausgabe.....
 
Original geschrieben von Vasquez
Code:
public Feld()
    {
	
	theCell = new Cell();
	for( int i=0; i<=19; i++)
	    {
		for( int j=0; j<=19; j++)
		    {
			meinFeld[i][j] = theCell;
			
		    }
	    }
    }
Du legst nur ein Cell Objekt mit theCell = new Cell(); an. In den Schleifen wird jedem Element deines Feldes die Referenz auf dieses eine Objekt zugewiesen. Jegliche Änderungen beziehen sich also auch immer nur auf dieses eine Objekt.

Um in jedem Element ein eigenes Objekt zu referenzieren, musst du auch jedes Mal ein neues Objekt erzeugen:
Code:
public Feld()
    {
	for( int i=0; i<=19; i++)
	    {
		for( int j=0; j<=19; j++)
		    {
			meinFeld[i][j] = new Cell();
		    }
	    }
    }
Peter
 

Neue Beiträge

Zurück