Reputation: 373
i have an arraylist of objects (list1) it contains seat_no, an image and its id. How do i iterate through list1 and store every data to another list. provided that the seat_no. matches the index of the another list (list2) which has a fixed size of 50. Should i create another object class? pls help...
example:
List1 contains
|seat_no | id | image |
======================================
| 1 | 001 | R.drawable.chair |
| 2 | 002 | R.drawable.chair |
| 3 | 003 | R.drawable.chair |
| 4 | 004 | R.drawable.chair |
| 7 | 005 | R.drawable.chair |
| 10 | 006 | R.drawable.chair |
List2 should contain
|index | id | seat_no |
=============================
| 1 | 001 | 1 |
| 2 | 002 | 2 |
| 3 | 003 | 3 |
| 4 | 004 | 4 |
| 5 |"blank" |"blank" |
| 6 |"blank" |"blank" |
| 7 | 005 | 7 |
| 8 |"blank" |"blank" |
| 9 |"blank" |"blank" |
| 10 | 006 | 10 |
here's my code
main.java
ArrayList<HashMap<String, String>> take = new ArrayList<HashMap<String, String>>();
try
{
JSONObject jsonArray = new JSONObject(result);
JSONArray info = jsonArray.getJSONArray("info");
list1 = new ArrayList<ViewGridObject>();
for (int i = 0; i < info.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = info.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("sid", e.getString("id"));
map.put("flag", Integer.toString(seat[0]));
map.put("seat_no", e.getString("seat_no"));
take.add(map);
list1.add(new ViewGridObject(e.getString("id"), R.drawable.chair, e.getString("seat_no")));
}
ViewGridObject.java
public class ViewGridObject
{
public String stud_id, seat_no;
int chair;
public ViewGridObject(String stud_id, int chair, String seat_no)
{
this.stud_id = stud_id;
this.chair = chair;
this.seat_no = seat_no;
}
}
Upvotes: 1
Views: 2570
Reputation: 425033
It's easier to create a new list:
List<ViewGridObject> list2 = new ArrayList<ViewGridObject>();
int i = 0;
for (ViewGridObject o : list1) {
for (i++; !o.seat_no.equals(i + ""); i++) {
list2.add(new ViewGridObject(null, 0, i + ""));
}
list2.add(o);
}
Upvotes: 1
Reputation: 41200
No. Java ArrayList
supports null value and it maintain insertion order. you can insert null where there is object available with index.
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
ex : -
you have this objects - ViewGridObject1,ViewGridObject2, ViewGridObject3, ViewGridObject4,ViewGridObject7;
list1.add(ViewGridObject1);
list1.add(ViewGridObject2);
list1.add(ViewGridObject3);
list1.add(ViewGridObject4);
list1.add(null);
list1.add(null);
list1.add(ViewGridObject7);
So your list will contain - obj1, obj2, obj3, obj4, null, null, obj7,...
Upvotes: 1