Education
Software Development Executive - I
Last updated on Jul 12, 2024
Last updated on Jul 12, 2024
In programming, arrays play a crucial role in storing and managing data. Dart arrays, also known as Dart lists, are an ordered group of items. The Dart programming language uses arrays to store multiple values in a single variable. This can be very useful when you want to group related values together.
In Dart, an array is a list object that can contain multiple values. These values can be accessed by their index number. The index starts from 0 for the first element and increases by one for each subsequent element. For example, if we have an array with five elements, the index of the first element will be 0, and the index of the last element will be 4.
Creating a new array in Dart is straightforward. We can use the new keyword, but it's not necessary. Dart allows us to create arrays with the literal constructor or by specifying values directly. For instance, we can create an empty array, an array with only one element, or an array with specified values.
1 void main() { 2 var array = []; // an empty array 3 var singleElementArray = [1]; // an array with only one element 4 var multiElementArray = [1, 2, 3, 4, 5]; // an array with specified values 5 } 6
In Dart, arrays can be of two types: fixed-length arrays (also known as non-growable arrays) and growable arrays. A fixed-length array has a length that can't change during runtime, while a growable array's length can change.
1 void main() { 2 var fixedLengthArray = List.filled(5, 0); // a fixed-length array of length 5, filled with zeros 3 var growableArray = []; // an empty growable array 4 } 5
Arrays in Dart have many useful properties and methods. For instance, the length property returns the total number of elements in the list. The first property returns the first element in the list, and the last property returns the last element in the list.
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 print(array.length); // prints: 5 4 print(array.first); // prints: 1 5 print(array.last); // prints: 5 6 } 7
In Dart, declaring and initializing arrays is a simple process. An array can be declared and initialized at the same time, or it can be declared first and then initialized later.
Here's an example of declaring and initializing an array in Dart:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; // declaring and initializing an array 3 } 4
In the above code, we declare a variable array and initialize it with an array of five elements. The var keyword is used to declare a variable, and the square brackets [] are used to denote an array.
An array can also be declared first and then initialized later:
1 void main() { 2 var array; // declaring an array 3 array = [1, 2, 3, 4, 5]; // initializing the array 4 } 5
Once an array is declared and initialized, we can access its elements using their index. Remember, in Dart, the index of the first element is 0, and it increases by one for each subsequent element.
Here's how to access elements in a Dart array:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 print(array[0]); // prints: 1 4 print(array[4]); // prints: 5 5 } 6
In the above code, we access the first element and the last element of the array using their indices.
The length property in arrays returns the total number of elements in the array. This can be useful when you need to know how many elements are in an array.
Here's how to use the length property in Dart:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 print(array.length); // prints: 5 4 } 5
In the above code, we print the length of the array, it counts all the elements inside the array, which is 5.
Adding elements to a Dart array is straightforward. Dart provides the add method to add a new element at the end of an array. Note that this method only works with growable arrays.
Here's how to add elements to a Dart array:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 array.add(6); // adding a new element to the array 4 print(array); // prints: [1, 2, 3, 4, 5, 6] 5 } 6
In the above code, we add a new element to the array using the add method, and then print the array.
Dart provides the remove method to remove a specific element from an array, and the removeAt method to remove an element at a specific index.
Here's how to remove elements from a Dart array:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 array.remove(3); // removing a specific element from the array 4 print(array); // prints: [1, 2, 4, 5] 5 6 array.removeAt(0); // removing an element at a specific index 7 print(array); // prints: [2, 4, 5] 8 } 9
In the above code, we first remove a specific element from the array using the remove method, and then remove an element at a specific index using the removeAt method.
To update an element in a Dart array, we can simply assign a new value to the element at a specific index.
Here's how to update elements in a Dart array:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 array[0] = 10; // updating an element at a specific index 4 print(array); // prints: [10, 2, 3, 4, 5] 5 } 6
In the above code, we update the first element of the array by assigning a new value to it.
A multidimensional array is an array of arrays. In Dart, we can create multidimensional arrays by nesting lists within lists.
Here's an example of a multidimensional array in Dart:
1 void main() { 2 var array = [ 3 [1, 2, 3], 4 [4, 5, 6], 5 [7, 8, 9] 6 ]; // a 3x3 multidimensional array 7 8 print(array); // prints: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 9 } 10
In the above code, we create a 3x3 multidimensional array and then print it.
Loops are a common way to iterate through the elements of an array. In Dart, we can use the for loop, the for-in loop, or the forEach method to iterate through an array.
Here's how to iterate through a Dart array using a for loop:
1 void main() { 2 var array = [1, 2, 3, 4, 5]; 3 4 for (var i = 0; i < array.length; i++) { 5 print(array[i]); // prints each element of the array 6 } 7 } 8
In the above code, we use a for loop to iterate through the array and print each element.
Dart provides the sort method to sort the elements of an array in ascending order, and the where method to filter the elements of an array based on a condition.
Here's how to sort and filter a Dart array:
1 void main() { 2 var array = [5, 3, 1, 4, 2]; 3 4 array.sort(); // sorting the array 5 print(array); // prints: [1, 2, 3, 4, 5] 6 7 var filteredArray = array.where((element) => element > 3); // filtering the array 8 print(filteredArray); // prints: (4, 5) 9 } 10
In the above code, we first sort the array in ascending order using the sort method and then create a new array that contains only the elements of the original array that are greater than 3, using the where method.
In this blog post, we've taken a comprehensive look at Dart arrays, from the basics of declaring and initializing arrays, accessing elements, and understanding the length property, to more advanced concepts like multidimensional arrays, iterating through elements with loops, and sorting and filtering arrays.
Dart arrays, or Dart lists as they are commonly referred to, are a fundamental part of the Dart programming language. They provide a way to store multiple values in a single variable, making it easier to group related values together. Whether you're creating a simple application or a complex system, you'll likely need to use arrays to store and manipulate data.
We've also seen how to perform common operations on arrays, such as adding, removing, and updating elements. We've learned how to use various properties and methods provided by Dart to work with arrays, such as the length property, the add method, the remove method, the sort method, and the where method.
Understanding arrays is essential for any Dart developer. With the knowledge gained from this blog post, you should now be able to create, manipulate, and work with arrays in Dart effectively.
Ready to dive deeper into Dart development and bring your app ideas to life?
DhiWise offers a platform that empowers developers to build stunning apps faster. With its intuitive interface and robust features, you can focus on the core logic while DhiWise handles the complexities.
Start building your next app today! Sign up with DhiWise and experience the future of app development.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.