Problem: Arraylist of objects, with objects which also have arraylist inside
I have an arraylist for some kind of objects. These objects, have some attributes like strings and some integers. But also, an arraylist of another kind of objects.
I create the 2nd object first. And group them into an arraylist of this kind of objects. Then I check if this arraylist is empty. If its not, then I create the other object, and pass it the first arrayList. And put it inside another arraylist. So at the end, I have an arraylist of one kind of objects. And all of these objects, also have an arraylist (and a few other values, like some strings and integers) with other objects.
The problem is that at the end, when I try to access the arraylist inside one of these objects, its empty.
Don't know if what I'm trying to do is impossible. I've declared both arraylists as arraylists of objects. Perhaps I should declare them as an arraylist of arraylists?
Edited:
Would be something like this:
class Obj1 {
String string;
int integer;
ArrayList<obj2> objects2;
public Obj1(String string, int integer, ArrayList<obj2> objects2) {
this.string = string;
this.integer = integer;
this.objects2 = objects2;
}
}
This would be the first object (its ArrayList objects2, but "<>开发者_StackOverflow社区;" doesn't appear).
class Obj2 {
String string;
int integer;
public Obj2(String string, int integer2) {
this.string = string;
this.integer = integer;
}
}
Same as before with the "<>"s.
Then, with an asynctask, I start to search from a webpage. I create some Ob2. I can have from 0 to maybe, 15. So if i get some, put all of them into an ArrayList of Objs2. Then I create an Obj1 and pass it its attributes string and integer, and the ArrayList of Objs2. And after, put this Obj1 into ArrayList of Objs1.
At the end, I can have for example, 12 Ojs1 into an ArrayList. And each one of these Obj1, have an ArrayList of Objs2, with 1 to 15 Objs2. What I return from the asynctask is the ArrayList of Objs1.
Edited 2:
private class DownloadWork extends AsyncTask`<URL, String, List<Obj1>>` {
ArrayList`<Obj1>` objs1Downloaded = new ArrayList`<Obj1>`();
ArrayList`<Obj2>` objs2Downloaded = new ArrayList`<Obj2>`();
//Some more stuff
protected List<Evento> doInBackground(URL... params) {
for(...) {
//Do some stuff here ...
}
return objs1Downloaded;
}
That objs1Downloaded, as I said, would have 12 objs1 for example. And each obj1 could have an arraylist of objs2, with 1 to 15 obj2. That's more or less the idea.
You should probably post some of your code to give us an idea, this shouldn't be any problem doing it like so:
class Inner {
String name;
int num;
ArrayList<String> stringList;
public Inner(String name, int num, ArrayList<String> items) {
this.name = name;
this.num = num;
this.stringList = items;
}
}
ArrayList<Inner> outerList = new ArrayList<Inner>();
ArrayList<String> innerList = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
innerList.add("" + i);
}
outerList.add(new Inner("Name", 1, innerList));
This gives you one ArrayList containing a single inner object, which contains another ArrayList of Strings*.
*untested, general idea should be good.
精彩评论