C
C#13mo ago
wcasa

✅ inputting list..

var size = int.Parse(Console.ReadLine());

List<int> arr = new List<int>(size);
for (int i = 0; i < size; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
var size = int.Parse(Console.ReadLine());

List<int> arr = new List<int>(size);
for (int i = 0; i < size; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
whats wrong with this code
13 Replies
ero
ero13mo ago
you tell us
wcasa
wcasa13mo ago
nukleer bomb
nukleer bomb13mo ago
List<T> length is zero when created
Jimmacle
Jimmacle13mo ago
the capacity of a list is not the same as its size
nukleer bomb
nukleer bomb13mo ago
That argument you are passing is capacity, not actual length
wcasa
wcasa13mo ago
List<int> arr = new List<int>(size); doesnt it say "create list with this size" ohhhh forgot those exist so i need to pushback them 1 by 1? is there more efficient solution? sounds slow..
nukleer bomb
nukleer bomb13mo ago
it's already efficient if you keep parameter in constructor
wcasa
wcasa13mo ago
var size = int.Parse(Console.ReadLine());

List<int> arr = new List<int> { };
for (int i = 0; i < size; i++)
{

int val = int.Parse(Console.ReadLine());
arr.Add(val);
}
var size = int.Parse(Console.ReadLine());

List<int> arr = new List<int> { };
for (int i = 0; i < size; i++)
{

int val = int.Parse(Console.ReadLine());
arr.Add(val);
}
so thats the way?
nukleer bomb
nukleer bomb13mo ago
It's efficient because List prepared some memory for size elements when constructor is called, but just didn't use it until Add is called
wcasa
wcasa13mo ago
so i should have that (size) at the end of the list?
nukleer bomb
nukleer bomb13mo ago
You can drop it, but it will be less efficient
wcasa
wcasa13mo ago
okie thanks men! !close
Accord
Accord13mo ago
Closed!
Want results from more Discord servers?
Add your server
More Posts
✅ SQLite Data Insert locks up databaseI am working on a project of mine and I can get my application to create the database and its tables❔ Serilog duplicate log with global exception handlinghey i am building an asp.net core web api and i implemented global exception handling like this: ``✅ What is the function of the Dictionary here?public class Solution { Dictionary<int,int>cached=new Dictionary<int,int>(); public int Cli❔ JWT Refresh tokensI've got a RefreshToken endpoint on my UserController class. I'm currently able to regenerate these ❔ System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=6.0.0.0Hey all I am designing a win forms application and when trying to test a build I am greeted with the❔ streaming an IAsyncEnumerable<string> from complex typehey. i have a minimal api that looks like this: ```cs public static async IAsyncEnumerable<string> A❔ Assign result from await to a variableIs there a difference between those two calls? `var awaitable = await GetObject(); otherObject.IntP❔ ✅ Prints System.Func`1[System.Int32] instead of actual int.I'm trying to return a private field member variable and instead of giving me the actual integer val❔ Exception trying to run code from a .NET Framework 4.5.2 on .NET 6 projectHi, I have a new project using .NET 6, but I need to call a method in a project using .NET Framewo✅ Hosting web app with SQL Server for free/minimal cost?Hello there, I was wondering if anyone knew any cloud/hosting providers that allow hosting an ASP.N