Is there an equivalent of Java's ArrayList in Dart?
Namely, I need a growable list which has these methods:
- size() method that returns the number of objects it holds.
- add(int index, E element) => inserts an element to a location,
shifts subsequent elements to right.
The one I use is Interface List<E>, where length would be equivalent to size() in Java, and cars.insertRange(1, 1, 'Ranger') would be used where you would use add(int index, E element) in java.
List<String> cars = new List<String>(); cars.
List<String> cars = new List<String>(); cars.add('Chevette'); cars.add('Corolla'); cars.add('Jetta'); cars[0] = 'Corvette';//replace a value at 0 cars.insertRange(1, 1, 'Ranger'); print('there are ${cars.length} cars. Car at index 1 is the ${cars[1]}');
On Thu, Apr 12, 2012 at 8:01 AM, Mehmet Akin <mda...@gmail.com> wrote: > Hi,
> Is there an equivalent of Java's ArrayList in Dart? > Namely, I need a growable list which has these methods:
> - size() method that returns the number of objects it holds. > - add(int index, E element) => inserts an element to a location, > shifts subsequent elements to right.
> > - size() method that returns the number of objects it holds.
> The length property.
Maybe i am missing something but It seems length is the number of
slots in the list, which is the equivalent of elementData.length in
Java Arraylist, not size of List.
> > - add(int index, E element) => inserts an element to a location,
> > shifts subsequent elements to right.
> Strictly speaking, there is no such method (and I hope that it will be),
> but insertRange is capable of doing it (and more).
Ok insertRange looks ok, though as you said, I would prefer a simpler
method.
> > > - size() method that returns the number of objects it holds.
> > The length property.
> Maybe i am missing something but It seems length is the number of > slots in the list, which is the equivalent of elementData.length in > Java Arraylist, not size of List.
I believe you can increase the size of a List, and each additional element will be assigned null by default. In this case, length would include all null values as well.
> > > - size() method that returns the number of objects it holds.
> > The length property.
> Maybe i am missing something but It seems length is the number of > slots in the list, which is the equivalent of elementData.length in > Java Arraylist, not size of List.
> > > - add(int index, E element) => inserts an element to a location, > > > shifts subsequent elements to right.
> > Strictly speaking, there is no such method (and I hope that it will be), > > but insertRange is capable of doing it (and more).
> Ok insertRange looks ok, though as you said, I would prefer a simpler > method.
On Apr 12, 1:41 pm, Ladislav Thon <ladi...@gmail.com> wrote:
> > > > - size() method that returns the number of objects it holds.
> > > The length property.
> > Maybe i am missing something but It seems length is the number of
> > slots in the list, which is the equivalent of elementData.length in
> > Java Arraylist, not size of List.
> and each additional element will be assigned null by default.
It will.
> In this case, length would include all null values as well.
Doh! I didn't think about that. My intuition tells me that it shouldn't, but on the other hand, what if you explicitly want to have nulls at the end of the list... I'll have to test that. Thanks for pointer!
On Apr 12, 2:47 pm, Allan MacDonald <amacdon...@cgsinc.ca> wrote:
> I believe you can increase the size of a List, and each additional element
> will be assigned null by default. In this case, length would include all
> null values as well.
> Allan
Indeed, from the documentation Dart Lists seems like a growable array.
There is no dictinction of length and capacity. Every insertion makes
the array length 1 more. And you can explicitly extend the length.
This gives more control in some cases but a distinction of length and
capacity would prevent some bugs (NPEs).
> > > > - size() method that returns the number of objects it holds.
> > > The length property.
> > Maybe i am missing something but It seems length is the number of
> > slots in the list, which is the equivalent of elementData.length in
> > Java Arraylist, not size of List.
> > > > - add(int index, E element) => inserts an element to a location,
> > > > shifts subsequent elements to right.
> > > Strictly speaking, there is no such method (and I hope that it will be),
> > > but insertRange is capable of doing it (and more).
> > Ok insertRange looks ok, though as you said, I would prefer a simpler
> > method.
On Apr 12, 2:10 pm, afsina <ahme...@gmail.com> wrote:
> On Apr 12, 2:47 pm, Allan MacDonald <amacdon...@cgsinc.ca> wrote:
> > I believe you can increase the size of a List, and each additional element
> > will be assigned null by default. In this case, length would include all
> > null values as well.
> > Allan
> Indeed, from the documentation Dart Lists seems like a growable array.
> There is no dictinction of length and capacity. Every insertion makes
> the array length 1 more. And you can explicitly extend the length.
> This gives more control in some cases but a distinction of length and
> capacity would prevent some bugs (NPEs).
On Thu, Apr 12, 2012 at 9:35 AM, Mehmet Akin <mda...@gmail.com> wrote:
> On Apr 12, 2:10 pm, afsina <ahme...@gmail.com> wrote: > > On Apr 12, 2:47 pm, Allan MacDonald <amacdon...@cgsinc.ca> wrote:
> > > I believe you can increase the size of a List, and each additional > element > > > will be assigned null by default. In this case, length would include > all > > > null values as well.
> > > Allan
> > Indeed, from the documentation Dart Lists seems like a growable array. > > There is no dictinction of length and capacity. Every insertion makes > > the array length 1 more. And you can explicitly extend the length. > > This gives more control in some cases but a distinction of length and > > capacity would prevent some bugs (NPEs).
> On Thu, Apr 12, 2012 at 9:35 AM, Mehmet Akin <mda...@gmail.com> wrote:
>> On Apr 12, 2:10 pm, afsina <ahme...@gmail.com> wrote: >> > On Apr 12, 2:47 pm, Allan MacDonald <amacdon...@cgsinc.ca> wrote:
>> > > I believe you can increase the size of a List, and each additional >> element >> > > will be assigned null by default. In this case, length would include >> all >> > > null values as well.
>> > > Allan
>> > Indeed, from the documentation Dart Lists seems like a growable array. >> > There is no dictinction of length and capacity. Every insertion makes >> > the array length 1 more. And you can explicitly extend the length. >> > This gives more control in some cases but a distinction of length and >> > capacity would prevent some bugs (NPEs).
On further examination, it appears that you can set existing list index variables to null. I suppose this is equivalent to ArrayList in Java since it will too accept null values.
Does anyone know if List has the equivalent of addAll(index,
collection)
for adding all the items in an existing collection to a list object at
the specified index?
List list = [1, 5, 9];
list.addAll(1, [2,3,4]);
print(list); // should print [1,2,3,4,5,9]
insertRange and setRange don't seem to do it.
I tried searching the open issues (advanced search , with words ...),
but I didn't get hits.
On Thu, Apr 12, 2012 at 10:55 AM, evan chua-yap <semweb...@gmail.com> wrote: > Does anyone know if List has the equivalent of addAll(index, > collection) > for adding all the items in an existing collection to a list object at > the specified index?
> List list = [1, 5, 9]; > list.addAll(1, [2,3,4]); > print(list); // should print [1,2,3,4,5,9]
> insertRange and setRange don't seem to do it. > I tried searching the open issues (advanced search , with words ...), > but I didn't get hits.
It doesn't seem to. I'm surprised insertRange() doesn't do this, but it looks like it doesn't. Soon, Josh is going to refresh our collection libraries and I'm 100% certain that this will be a supported operation when he does.
Yep. I've been very busy with the leadup to next week's trial (Oracle v. Google; looks like I'll be testifying early in the week, and then again the week after). But I will get this done as soon as possible.
On Thu, Apr 12, 2012 at 11:04 AM, Bob Nystrom <rnyst...@google.com> wrote: > On Thu, Apr 12, 2012 at 10:55 AM, evan chua-yap <semweb...@gmail.com>wrote:
>> Does anyone know if List has the equivalent of addAll(index, >> collection) >> for adding all the items in an existing collection to a list object at >> the specified index?
>> List list = [1, 5, 9]; >> list.addAll(1, [2,3,4]); >> print(list); // should print [1,2,3,4,5,9]
>> insertRange and setRange don't seem to do it. >> I tried searching the open issues (advanced search , with words ...), >> but I didn't get hits.
> It doesn't seem to. I'm surprised insertRange() doesn't do this, but it > looks like it doesn't. Soon, Josh is going to refresh our collection > libraries and I'm 100% certain that this will be a supported operation when > he does.