ArrayList Issue

Gosre

I have two values, small_red and small_blue:

private EnemyInfo small_red = new EnemyInfo("Red Fighter", Core.jf.getToolkit().createImage(Core.class.getResource("/com/resources/ENEMY_01.png")), 10, 100, new Location(0, 0), false, 0);
private EnemyInfo small_blue = new EnemyInfo("Blue Fighter", Core.jf.getToolkit().createImage(Core.class.getResource("/com/resources/ENEMY_02.png")), 50, 100, new Location(0, 0), false, 0);

and an ArrayList:

private ArrayList<EnemyInfo> activeEnemies = new ArrayList<EnemyInfo>();

Let's say I add three of the small_red and five of the small_blue enemies into the activeEnemies. Whenever I want to change a variable inside the array, e.g.:

activeEnemies.get(1).setActive(true); // change one of the small_red enemies

every small_red in the array is changed, instead of just the one at index 1.

Richard Tingle

You're adding 3 references to the same smallRed enemy each time to the arraylist.

To explain;

private EnemyInfo small_red; //I am a variable, I hold a reference to an EnemyInfo

new EnemyInfo(.....) //I create a new EnemyInfo object "somewhere" and return a reference to it so it can be used.

small_red can be considered a memory address (although its more complicated than that), so you're adding the same memory address several times (like adding the same house address to your real life address book). It doesn't matter what page you get the address from in your address book; letters go to the same house.

Every time you use the new keyword you are creating a new instance of an object, otherwise you're just passing around a reference to an old object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Arraylist override values issue?

From Dev

Arraylist of String Arrays issue

From Dev

Java ArrayList serialization issue

From Dev

ArrayList method logic issue

From Dev

An issue with Dictionary and ArrayList

From Dev

Arraylist and JTextArea Display issue

From Dev

Android Issue Logging ArrayList

From Dev

indexOf(), arrayList issue java

From Dev

Issue calling a method from an ArrayList

From Dev

Factory Pattern/ArrayList/Interface Issue

From Dev

Concurrent Threads and Subsequent Issue with ArrayList

From Dev

Index issue on arraylist (Android Studio)

From Dev

Remove duplicates from ArrayList issue

From Dev

issue in adding common elements of arraylist in third arraylist using for loop

From Dev

Java ArrayList issue with try-catch

From Dev

Issue with Scanner and While loop to check an arraylist

From Dev

Beginner Java remove object in an ArrayList Java Issue

From Dev

Strange issue with writing/reading ArrayList to a file

From Dev

Java ArrayList.remove(index) issue

From Dev

issue to pass arraylist from an intent to another

From Dev

ArrayList issue - Calculating Airline mile shares

From Dev

Possible logic issue adding an object to an arraylist

From Dev

Web Api Get FromUri - ArrayList Issue

From Dev

Arraylist issue ArrayListName.add(new ArrayListName(.......));

From Dev

Passing ArrayList to PHP using Android Asynchronous Http Client Issue

From Dev

Java: Issue With Storing Data from Excel in an ArrayList of ArrayLists

From Dev

Expected BEGIN_ARRAY but was BEGIN_OBJECT | ArrayList issue

From Dev

Java Arraylist issue change at one place leads to another place

From Dev

Java: Issue With Storing Data from Excel in an ArrayList of ArrayLists

Related Related

HotTag

Archive