Last active
October 1, 2024 08:25
-
-
Save imhemish/b7992921115777e494c672d6c1f90207 to your computer and use it in GitHub Desktop.
Doubt session 28/9/24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:io'; | |
| void main() { | |
| List<int> listOfNumbers = []; | |
| late String inputFromUser; | |
| while (true) { | |
| print("Enter the operation to do: "); | |
| print("1. Add Element"); | |
| print("2. Delete Element"); | |
| print("3. Search Element"); | |
| print("4. Exit"); | |
| print("5. Display elements"); | |
| String option = stdin.readLineSync()!; | |
| switch(option) { | |
| case "1": | |
| print("Enter the value to add: "); | |
| inputFromUser = stdin.readLineSync()!; | |
| listOfNumbers.add(int.parse(inputFromUser)); | |
| break; | |
| case "2": | |
| print("Enter the value to delete: "); | |
| inputFromUser = stdin.readLineSync()!; | |
| listOfNumbers.remove(int.parse(inputFromUser)); | |
| break; | |
| case "3": | |
| print("Enter the value to search: "); | |
| inputFromUser = stdin.readLineSync()!; | |
| int Index = listOfNumbers.indexOf(int.parse(inputFromUser)); | |
| if (Index == -1) { | |
| print("Item does not exist"); | |
| } else { | |
| print("The item is at index: "+Index.toString()); | |
| } | |
| break; | |
| case "4": | |
| exit(0); | |
| case "5": | |
| print(listOfNumbers); | |
| break; | |
| default: | |
| print("Invalid input"); | |
| break; | |
| // .remove(value) | |
| // [1 2 3 4 5] remove(3) [1 2 4 5] | |
| // [1 2 3 4 5] removeAt(3) [1 2 3 5] | |
| // [1 2 3 4 5] removeLast() [1 2 3 4] | |
| // [1 2 3 4 5] indexOf(3) => 2 | |
| // indexOf(6) => -1 | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| List<int> numbers = [7, 8, 9, 8, 9]; | |
| int firstIndex = numbers.indexOf(9); | |
| print("The second occurence of 9 is at: "); | |
| print(numbers.sublist(firstIndex+1).indexOf(9) + firstIndex + 1); | |
| // [7, 8, 9, 8, 9] firstIndex = 2 | |
| // sublist(2) => [9,8,9] | |
| // sublist(3) => [8,9] | |
| //[8, 9] => indexOf(9) => 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment