Das Erstellen einer ArrayList von Ganzzahlarrays führt zu doppelten EinträgenJava

Java-Forum
Anonymous
 Das Erstellen einer ArrayList von Ganzzahlarrays führt zu doppelten Einträgen

Post by Anonymous »

Ich versuche ein einfaches Spiel zu schreiben, bei dem ein Feind den Spieler in ein Netz jagt. Ich benutze den einfachen Algorithmus zum Pfadfinding von der Wikipedia -Seite auf dem Pfadfinding. Dies beinhaltet das Erstellen von zwei Listen mit jedem Listenelement mit 3 Zahlen. Hier ist der Testcode, den ich versuche, eine solche Liste zu erstellen und anzuzeigen. Warum macht es das? < /P>

Code: Select all

public class ListTest {

public static void main(String[] args) {
ArrayList list = new ArrayList();
Integer[] point = new Integer[3];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
point[j] = (int)(Math.random() * 10);
}

//Doesn't this line add filled Integer[] point to the
//end of ArrayList list?
list.add(point);

//Added this line to confirm that Integer[] point is actually
//being filled with 3 random ints.
System.out.println(point[0] + "," + point[1] + "," + point[2]);
}
System.out.println();

//My current understanding is that this section should step through
//ArrayList list and retrieve each Integer[] point added above. It runs, but only
//the values of the last Integer[] point from above are displayed 10 times.
Iterator it = list.iterator();
while (it.hasNext()) {
point = (Integer[])it.next();
for (int i = 0; i < 3; i++) {
System.out.print(point[i] + ",");
}
System.out.println();
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post