Home > Enterprise >  Is an array and a List the same thing in Dart?
Is an array and a List the same thing in Dart?

Time:06-13

I read the following quote in here:

Perhaps the most common collection in nearly every programming language is the array, or ordered group of objects. In Dart, arrays are List objects, so most people just call them lists.

Does that mean the following two statements are the same?:

var names = ["John", "Robert", "James"];

List<String> names = ["John", "Robert", "James"];

CodePudding user response:

The Dart List type is what most other languages call "arrays" or "vectors". That is, it is a data structure that stores elements contiguously to provide O(1) random access.

Dart uses [ and ] to create List literals.

Does that mean the following two statements are the same?:

var names = ["John", "Robert", "James"];

List<String> names = ["John", "Robert", "James"];

Yes, they are the same thing. DartPad or a Dart-aware IDE will let you inspect the static types of variables to show you that.

  •  Tags:  
  • dart
  • Related