Dart Runes elementAt()
Syntax & Examples
Runes.elementAt() method
The `elementAt` method in Dart returns the index-th element.
Syntax of Runes.elementAt()
The syntax of Runes.elementAt() method is:
int elementAt(int index)
This elementAt() method of Runes returns the index
th element.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
index | required | The index of the element to retrieve. |
Return Type
Runes.elementAt() returns value of type int
.
✐ Examples
1 Accessing an element by index from a list of numbers
In this example,
- We create a list
numbers
containing integers. - We use the
elementAt()
method with index 1 to retrieve the second element. - We then print the retrieved element to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3];
int element = numbers.elementAt(1);
print(element);
}
Output
2
2 Accessing an element by index from a list of characters
In this example,
- We create a list
characters
containing characters. - We use the
elementAt()
method with index 2 to retrieve the third element. - We then print the retrieved element to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c'];
String element = characters.elementAt(2);
print(element);
}
Output
c
3 Accessing an element by index from a list of strings
In this example,
- We create a list
words
containing strings. - We use the
elementAt()
method with index 0 to retrieve the first element. - We then print the retrieved element to standard output.
Dart Program
void main() {
List<String> words = ['hello', 'world'];
String element = words.elementAt(0);
print(element);
}
Output
hello
Summary
In this Dart tutorial, we learned about elementAt() method of Runes: the syntax and few working examples with output and detailed explanation for each example.