1

Tabular Inputs This suggests to use XWorkList instead of ArrayList, when the size of array is unknown & there are gaps in between.

But XWorkList is not generic & it has no empty constructor, according to documentation.

My question is how to use XWorkList or is there any way to submit list of beans with some items missing in the list ?

Sample Html:

<input name="lst[0].name"/>
<input name="lst[3].name"/>
<input name="lst[4].name"/>
Roman C
  • 49,761
  • 33
  • 66
  • 176
coding_idiot
  • 13,526
  • 10
  • 65
  • 116

2 Answers2

0

A List contract is an ordered collection. It can't have missing indeces. About XWorkList it's just an other implementation of the list, capable of creating new elements for the specified index by filling gap for required elements. What is said for add(int, Object)

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). This method is guaranteed to work since it will create empty beans to fill the gap between the current list size and the requested index to enable the element to be set.

Another approach for creating new beans in the list is to use type conversion. Or using annotations like in this answer.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • well in that case, ArrayList also does the same, it creates null for missing entries. Is there any way, I can prevent these null-entries from being created or remove them ? – coding_idiot Dec 07 '13 at 08:49
  • No, if you specify index greater than size of `ArrayList` it will throw `IndexOutOfBoundsException`. – Roman C Dec 07 '13 at 08:58
0

It's extending ArrayList and has a constructor that takes a Class. Thus you would change:

List<String> foo = new ArrayList<String>();
foo.add("bar");
foo.add("");
foo.add("");
foo.add("foobar");
foo.add("barfoo");

into:

List<String> foo = new XWorkList(String.class);
foo.add("bar");
foo.add(3, "foobar");
foo.add(4, "barfoo");
t0mppa
  • 3,983
  • 5
  • 37
  • 48