Reputation: 1758
I am interestig in how to get value from Object
from List<>
.
Here is code example with Objects
@Override
public List<ListObject> initChildren() {
//Init the list
List<ListObject> mObjects = new ArrayList<ListObject>();
//Add an object to the list
StockObject s1 = new StockObject(this);
s1.code = "Системне програмування-1";
s1.num = "1.";
s1.value = "307/18";
s1.time = "8:30 - 10:05";
mObjects.add(s1);
StockObject s2 = new StockObject(this);
s2.code = "Комп'ютерна електроніка";
s2.num = "2.";
s2.value = "305/18";
s2.time = "10:25 - 11:00";
mObjects.add(s2);
StockObject s3 = new StockObject(this);
s3.code = "Психологія";
s3.num = "3.";
s3.value = "201/20";
s3.time = "11:20 - 13:55";
mObjects.add(s3);
StockObject s4 = new StockObject(this);
s4.code = "Проектування програмного забезпечення";
s4.num = "4.";
s4.value = "24";
s4.time = "14:15 - 16:50";
mObjects.add(s4);
return mObjects;
}
Upvotes: 0
Views: 107
Reputation: 1271
You can iterate over the collection and type cast to the data type you know its in there.
List<ListObject> listOfObjects = initChildren();
for (Iterator iterator = listOfObjects.iterator(); iterator.hasNext();) {
StockObject so = (StockObject) iterator.next();
// do whatever you want with your StockObject (so)
System.out.println("Code:" + so.code);
}
You can use for each syntax as well like following
List<ListObject> listOfObjects = initChildren();
for (ListObject listObject : listOfObjects) {
StockObject so = (StockObject) listObject;
// do whatever you want with your StockObject (so)
System.out.println("Code:" + so.code);
}
Upvotes: 0
Reputation: 2732
you use like below
List<ListObject> mObjects = new ArrayList<ListObject>();
.......................your program...........
//access using enhanced for loop
for(ListObject myObj : mObjects){
System.out.println(myObj.code);
System.out.println(myObj.num);
System.out.println(myObj.value);
}
//access using index
int index=0;
System.out.println(mObjects.get(index).code);
Upvotes: 0
Reputation: 601
You can use get() method like follows
mObjects.get(index)
where index is the zero based index of your List, just like an array. To directly access object, you do for example,
mObjects.get(index).code
Upvotes: 1