Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Indexes in Dart | Variables and Data Types in Dart
Introduction to Dart

bookIndexes in Dart

Every element in a List or String has a unique address called an index. Indexes start from 0 the first element has index 0, the second has index 1, and so on.

You can retrieve an element from the list using its index with the following syntax.

main.dart

main.dart

copy
1234
void main() { List<String> names = ["Bob", "John"]; print(names[1]); // John }

Dart allows modifying the value of an item in a List. In other words, you can re-write the value of a List item.

main.dart

main.dart

copy
12345
void main() { List cars = ["BMW", "Porsche", "Ford"]; cars[0] = "Toyota"; print(cars); // ["Toyota", "Porsche", "Ford"] }

The above example updates the value of the List item with index 0. The output of the code will be − ["Toyota", "Porsche", "Ford"]. A String also have indexes.

main.dart

main.dart

copy
1234
void main() { String user = "Alex"; print(user[0]); }
Note
Note

The only difference between Indexes in a String and Indexes in a List is that in lists, we can update values at those addresses, whereas in strings, we cannot.

1. Select the element at index 1 in the string 'Codefinity'.

2. What will the following code output?

question mark

Select the element at index 1 in the string 'Codefinity'.

Select the correct answer

question mark

What will the following code output?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 7

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how to access an element in a list using its index?

How do I update a value in a list by its index?

Can you show an example of accessing a character in a string by its index?

Awesome!

Completion rate improved to 4.55

bookIndexes in Dart

Sveip for å vise menyen

Every element in a List or String has a unique address called an index. Indexes start from 0 the first element has index 0, the second has index 1, and so on.

You can retrieve an element from the list using its index with the following syntax.

main.dart

main.dart

copy
1234
void main() { List<String> names = ["Bob", "John"]; print(names[1]); // John }

Dart allows modifying the value of an item in a List. In other words, you can re-write the value of a List item.

main.dart

main.dart

copy
12345
void main() { List cars = ["BMW", "Porsche", "Ford"]; cars[0] = "Toyota"; print(cars); // ["Toyota", "Porsche", "Ford"] }

The above example updates the value of the List item with index 0. The output of the code will be − ["Toyota", "Porsche", "Ford"]. A String also have indexes.

main.dart

main.dart

copy
1234
void main() { String user = "Alex"; print(user[0]); }
Note
Note

The only difference between Indexes in a String and Indexes in a List is that in lists, we can update values at those addresses, whereas in strings, we cannot.

1. Select the element at index 1 in the string 'Codefinity'.

2. What will the following code output?

question mark

Select the element at index 1 in the string 'Codefinity'.

Select the correct answer

question mark

What will the following code output?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 7
some-alt