3

Hello I have a Class for Global Variables. One of the Functions within that Class is used to clear one of my ArrayList's. The only issue, is that it is clearing Two of my ArrayLists and I can't figure out why. All it is suppose to clear is m_listItems.

Before the Clear Runs: https://i.stack.imgur.com/dN5n4.png

After the Clear Runs: https://i.stack.imgur.com/5eKfj.png

This function adds the ArrayList "m_listItems" to the double Array List.

public void addGrouptoList(){
    ArrayList myArray = Global.m_listItems;
    Global.groupCollection.add(myArray);
}

This function should clear ONLY m_listItems but it clears both:

public void ClearNameList(){
    Global.m_listItems.clear();
}

This is how I created the two ArrayList's

private static ArrayList<ArrayList<String>> groupCollection= new ArrayList<>(); 
private static ArrayList<String> m_listItems = new ArrayList<String>();

Any Help would be much appreciated. Whether it be a work around or why its messing up!

EDIT I have done a lot of googling and I think the issue is because I'm doing add(global.m_list_items), so when i clear m_list_items, it clears it in my other array also... is there a work around for this? I read that Static is what is causing the issue but I need that for my global variable. I can't seem to find a workaround in google.

Thanks!

braX
  • 11,506
  • 5
  • 20
  • 33
  • why did you make arraylist as global? – Rashid Jun 29 '16 at 01:56
  • The double Array List stores Groups which stores the names of each group member. I needed to access this data across activities and the only way I could find on doing this is with a Global Class. I'm sure their are much better ways to do this (and I'm willing to listen), but I am a novice programmer for Java android and am doing a class project. – Joey Vagedes Jun 29 '16 at 02:15

2 Answers2

3

array don't holds objects but pointers to objects making new array and adding object to the second array don't make a copy of objects ! Modifying object in one array you are modifying it in the second

u need to do

new ArrayList<>(oldArray);

read more about shallow and deep copy

in example above we use a copy constructor other method is to use

http://cs-fundamentals.com/java-programming/java-arrays-clone-shallow-deep-copy.php

How do I copy an object in Java?

Deep copy, shallow copy, clone

btw:

i like your style :) total violation of all rules & conventions :) if u need help cal me anytime !!!

Community
  • 1
  • 1
ceph3us
  • 7,326
  • 3
  • 36
  • 43
  • I will probably take you up on that offer :) Unfortunately I am not violating all rules and conventions on purpose ! I am in a Software Engineering course where the purpose of the class is to practice the development lifecycle of software. This means that the entire class is basically developing an app.... except I have no java experience. So I've had to teach myself and google everything so far and I must say it has not been a pleasant experience! I appreciate the help because I was stuck on that issue for ~5 hours :( – Joey Vagedes Jun 29 '16 at 02:30
  • @JoeyVagedes dont hesitate to ask its nice to see you trying to learn :) we all constantly learning by our whole life. Java is nice language try with basics and you will see its easy – ceph3us Jun 29 '16 at 02:51
0

Global.m_listItems is the only object, so when you add it to Global.groupcollections you are not creating a new object, you are only pointing it to the same object. Hence when you destroy the object in one place itGlobal.m_listItems, you are left with no object at all. You need to create a new object, that has the same data as Global.m_listitems.