ArrayList equivalent?

2,545 views
Skip to first unread message

Mehmet Akin

unread,
Apr 12, 2012, 7:01:35 AM4/12/12
to General Dart Discussion
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.

Thanks

Allan MacDonald

unread,
Apr 12, 2012, 7:31:34 AM4/12/12
to Mehmet Akin, General Dart Discussion
Hi,

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]}');

This prints out;

there are 4 cars. Car at index 1 is the Ranger

Allan

Ladislav Thon

unread,
Apr 12, 2012, 7:32:10 AM4/12/12
to Mehmet Akin, General Dart Discussion
Is there an equivalent of Java's ArrayList in Dart?

 
- size() method that returns the number of objects it holds.

The length property.
 
- 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).

LT

Mehmet Akin

unread,
Apr 12, 2012, 7:38:41 AM4/12/12
to General Dart Discussion
Hi,

On Apr 12, 1:32 pm, Ladislav Thon <ladi...@gmail.com> wrote:
> > Is there an equivalent of Java's ArrayList in Dart?
>
> Yes, it's called List (http://api.dartlang.org/dart_core/List.html).
>
> > - 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.

>
> LT

Thanks

Mehmet

Ladislav Thon

unread,
Apr 12, 2012, 7:41:53 AM4/12/12
to Mehmet Akin, General Dart Discussion
> > - 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.

Very unlikely. I can't test now, but I'm pretty sure I already used List.length and it works as expected. See more at http://api.dartlang.org/dart_core/Collection.html#get:length

LT 

Allan MacDonald

unread,
Apr 12, 2012, 7:47:14 AM4/12/12
to Mehmet Akin, General Dart Discussion
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

Mehmet Akin

unread,
Apr 12, 2012, 7:55:25 AM4/12/12
to General Dart Discussion
Hi,
Ok, you are right , it seems I messed up something else. I guess this
is the default growable list implementation:
http://code.google.com/p/dart/source/browse/trunk/dart/runtime/lib/growable_array.dart

Thanks everyone answered.

Ladislav Thon

unread,
Apr 12, 2012, 8:03:11 AM4/12/12
to Allan MacDonald, Mehmet Akin, General Dart Discussion
I believe you can increase the size of a List,

Sure you can.
 
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!

LT

afsina

unread,
Apr 12, 2012, 8:10:41 AM4/12/12
to General Dart Discussion

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).

Mehmet Akin

unread,
Apr 12, 2012, 8:35:47 AM4/12/12
to General Dart Discussion

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).
>

Actually if you construct the list with inital length it is fixed
length; Otherwise it is growable. (Explained here:
http://blog.sethladd.com/2011/12/lists-and-arrays-in-dart.html).


Allan MacDonald

unread,
Apr 12, 2012, 8:41:23 AM4/12/12
to Mehmet Akin, General Dart Discussion
Ah, yes. This is a good point.

Allan 

Allan MacDonald

unread,
Apr 12, 2012, 8:50:09 AM4/12/12
to Mehmet Akin, General Dart Discussion
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. 

Allan 


evan chua-yap

unread,
Apr 12, 2012, 1:55:09 PM4/12/12
to General Dart Discussion
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.

Thanks!

evan

Bob Nystrom

unread,
Apr 12, 2012, 2:04:42 PM4/12/12
to evan chua-yap, General Dart Discussion
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.

- bob

Joshua Bloch

unread,
Apr 12, 2012, 2:10:21 PM4/12/12
to Bob Nystrom, evan chua-yap, General Dart Discussion
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.

    Josh
Reply all
Reply to author
Forward
0 new messages