I’m trying to make a tab system in my app which allows the user to filter which content gets shown in each tab (i.e. the user chooses which items to hide). All the items are available in an array: ArrayList<ItemInfo> mItems. In my TabInfo class I would like to have something like this:
public class TabInfo {
public int numItems;
public ArrayList<ItemInfo> content;
}
The content array just mItems filtered down. The problem with this is that I have to keep a copy of many of the ItemInfo‘s and it will waste a lot of memory (there could be duplicate items between the tabs) and it’s all just contained in mItems.
Is there a way to have a filtered mItems in the TabInfo without keeping duplicates?
Unless you’re doing something to make the
contentarrays contain copies of yourItemInfos, they won’t. The ArrayLists store references to objects, not objects directly.When you insert the same object into multiple
ArrayLists, the object isn’t copied, multiple references are stored.So you don’t have to do anything. (Or, if you’re copying/cloning your objects before storing them, you need to stop doing that.)