I have a snippet of code (below) that when run, will (ideally) fill an app array to look something like this:
Header 1:
App 1
App 2
Header 2:
App 3
App 4
etc.
However, when I run it in my emulator, it produces this:
Header 1:
App 1
App 2
App 3
App 4
...
Header 2:
App 1
...
I'm not sure why this happens, as I have looked up the defaults and everywhere I look, people say that Java defaults to passing by value, not by reference. If this is true, how come it adds every app to every category?
Preconditions:
Store is defined as a list of class "App", each element containing a header and title, among other things.
ListDataChild is an empty hashmap, with types > to be filled by the loop and then outputted via an expandable list view. I would post the code for the view, but it is very bulky and long so I'll just add a simple testing algorithm at the end.
EmptyList is just that, a list of type that has nothing inside (at least to begin with? Could it be changing?)
Code:
listDataChild = new HashMap<String, List<App>>();
List<App> emptylist = new ArrayList<>();
List<App> Store = synced.getAppStore();
Boolean juststarted = true;
for (App el : Store)
{
if (juststarted)
{
juststarted = false;
listDataChild.put(el.getHeader(), emptylist);
listDataChild.get(el.getHeader()).add(el);
} else {
if (listDataChild.containsKey(el.getHeader())) {
listDataChild.get(el.getHeader()).add(el);
} else {
listDataChild.put(el.getHeader(), emptylist);
listDataChild.get(el.getHeader()).add(el);
}
}
}
//TESTING
for (String header : listDataChild.keySet())
{
for (int j = 0; j < listDataChild.get(header).size(); j++)
{
System.out.println("HashMap at " + header + ", " + j + ": " + listDataChild.get(header).get(j).getTitle());
}
}
App.Java:
public class App {
public String header,title,link,url,redirect,icon;
public Double order;
public Boolean selected;
public App() {
header = "";
title = "";
link = "";
url = "";
redirect = "";
icon = "";
order = 0.0;
selected = false;
}
public int compareTo(App another) {
if (this.getOrder()<another.getOrder()){
return -1;
}else{
return 1;
}
}
public void setHeader(String h) {
header = h;
return;
}
public void setTitle(String t) {
title = t;
return;
}
public void setLink(String l) {
link = l;
return;
}
public void setUrl(String u) {
url = u;
return;
}
public void setRedirect(String r) {
redirect = r;
return;
}
public void setIcon(String i) {
icon = i;
return;
}
public void setOrder(Double o) {
order = o;
return;
}
public void setSelected(Boolean s) {
selected = s;
return;
}
public String getTitle()
{
return title;
}
public String getHeader()
{
return header;
}
public Double getOrder()
{
return order;
}
}
Thanks in advance!