I am trying to reduce the amount of memory being used in a Java project I'm working on, and one of the thing I'm looking into is using iterators of arrays, rather that copies of arrays. However, I'm not sure if this will work or not with private data inside of an object. For instance:
public class MyClass {
private ArrayList<String> data;
public ArrayList<String> getData(){ return this.data; }
public Iterator getDataIter(){ return this.data.iterator(); }
}
public static void main(String[] args) {
MyClass c = MyClass();
ArrayList<String> copyOfData = c.getData();
Iterator dataIter = c.getDataIter();
}
As you can see, in main() I couldn't just go c.data because it is private. However, c.getData() will copy the whole array. So here are my questions:
- Will dataIter be able to iterate over the values in data, even though it is a private variable?
- Will doing this save me any memory because I'm not passing large arrays around anymore?