Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
2. Basic types: List
//2. Basic types: List using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //The most used non-primitive type is a List. List<int> list = new List<int>(); //list of int //same as List<int> list = new List<int>() {1,5,3}; list.Add(1); //O(1) (when enough peallocated space), O(n) (when new space needs to be allocated) list.Add(5); list.Add(3); Console.WriteLine(list[0]); //get item by index - O(1) for(int i=0; i < list.Count; i++) { Console.Write(list[i]+" "); //iterate over list } Console.WriteLine(); Console.WriteLine(list.Contains(3)); //linear search - O(n) list = list.OrderBy(f => f).ToList(); //sort list Console.WriteLine(list.BinarySearch(3)); //binary search on sorted list - O(logn), returns 0-based index of found item, or negative number when not found //filter elements that satisfy some condition list = list.Where(f => f > 1).ToList(); list.ForEach(f => Console.Write(f+" ")); //another way to iterate over list } } }
run
|
edit
|
history
|
help
0
reverse array
Delegates
Prime Fibonacci
Int number to String Array
asxsadxsdf
testing endpoint
1
simultQ
Date example
Check if string is palindrome