Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to make a lot of array in for loops by C# like below: please help me!
for(int i=1;i<10;i++)
{
int[][] a+i=new int[10][3];
}
IMO, best will be to use a Dictionary<string,int[][]>.
During creation you will place a new array (which you just created) and assosiate it to the key "a" + i.
To get this array, just get the value attached to the relevant key.
Something like (C#-like pseudo code):
var map = new Dictionary<string,int[][]>();
for(int i=1;i<10;i++)
{
var temp = new int[10][3];
map["a" + i] = temp
}
and to get a value just use map[key] (for example map["a7"] will get the 7th element).
A good alternative would be to use a 3D array.
You are probably trying to create jagged arrays. Here's how you can do it:
var a = new int[10][];
for (var i = 0; i < a.Length; i++)
a[i] = new int[3];
for (var i = 0; i < a.Length; i++)
for (var j = 0; j < a[i].Length; j++)
a[i][j] = 1; // Initialize with your values
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In the below code i have declared an and then assigned it to z. [Please note this is a duplicate]
double an;
for(double z= Amt+Intrest; z>0; z-=MonthlyInstallment)
{
an = z;
}
I want to access the an variable in the below loop.
for (int i = 0; i < Months; i++)
{
dt.Rows.Add(i + 1, MonthlyInstallment, MonthlyIntrest, MonthlyInstallment - MonthlyIntrest,//use the an variable here);
}
You cannot relate the two loops directly, because I assume that those have equal iterations.
A suggestion: You could calculate the value a the loop.
// start with the base value.
double an = Amt + Intrest;
for (int i = 0; i < Months; i++)
{
dt.Rows.Add(i + 1, MonthlyInstallment, MonthlyIntrest, MonthlyInstallment -MonthlyIntrest, an);
// decrease an with the MonthlyInstallment
an = an - MonthlyInstallment;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c # i'm trying to add two different integer name, is it possible
int variablename;
for(int i=0;i<10;i++)
{
Objectname.variablename+i
}
No, you cant. C# is strongly typed programming language. What you can do, is to use Dictionary for example, like this:
Dictionary<string, int> names = new Dictionary<string,int>();
for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}
var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
If I understand you correctly you want to get the name of your variable and then change the name for each instance based on the iteration, thus you want to add a numeric value to the name. You can achieve this as follows:
string testVariable = "";
int i = 1;
// Get the name of your variable. This will result in "testVariable" as value.
string nameOfTestVariable = nameof(testVariable);
// Now add your integer to the name, but add it as a string.
nameOfTestVariable += i.ToString();
//Result, value of nameOfTestVariable: "testVariable1"
To get the datatype of your variable instead:
int a = 0;
int b = 1;
string dataTypesOfVariables = a.GetType().Name;
dataTypesOfVariables += b.GetType().Name;
//Result, value of dataTypesOfVariables: "Int32Int32"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
how to get the fastest result
i write the code below.
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256); j++)
for (int k = 0; k < 256; k++)
for (int p = 0; p < 256; p++)
{
writer.WriteLine(string.Format("{0}.{1}.{2}.{3}", i, j, k, p));
}
but my users told me that it is dammed slow. i dont have any idea how to boost the progress. share the problem, maybe
someone knows that. thanks.
You can try with IPAddressRange : https://www.nuget.org/packages/IPAddressRange/
But it will still be very long if you want to get all the ipv4 range!
var range = NetTools.IPAddressRange.Parse("192.168.0.10 - 192.168.10.20");
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (var item in range)
{
builder.Append(item);
}
You are saving 256 to the power of 4 items. That is over 4 billion calls to setText. If you need to create that many items, you have to look into the performance of setText. Your loop is performant enough and if you can optimize setText is unclear because I don't know what it does. But anything you do 4 billion times will be probably slow.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Getting a byte array like this [0,0,0,0,0,1,1,1,1,3,3,3,3,0,0,0,0,0]
Does anyone know how to detect a change from 1 to 3 efficiently in linq?
Why Linq? you could achieve this with simple loop.
int previous = array[0];
for(int i=1;i< array.Length;i++)
{
if(Math.Abs(array[i]- previous) > 1) // use appropriate jump
{
//logic
}
previous = array[i];
}
If you are looking for Linq solution, you could do this.
int previous = array[0];
array.FirstOrDefault(x=>
{
var retValue = Math.Abs(x- previous) > 1;
previous = x;
return retValue;
});
If you want to find all change-indexes in the most efficient way:
List<int> changeIndexes = new List<int>();
for(int i = 1; i < array.Length; i++)
if(array[i] != array[i-1])
changeIndexes.Add(i);
Linq is not the best tool when it comes to indexes.
If you want to find only the index where 1 changes to 3 modify the condition accordingly:
if(array[i] == 3 && array[i-1] == 1) ... // break the loop if you only want to find the first
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this list List<string> Url = new List<string>(); with more element >10 and i need to get only 5 element.
I tried in this mode i get all element:
foreach (string key3 in Url)
{
listBox3.Items.Add(key3);
}
IEnumerable<string> firstFiveUrls = Url.Take(5);
Enumerable.Take documentation
So you could do:
// ObjectCollection.AddRange expects an array
listBox3.Items.AddRange(Url.Take(5).ToArray());
ObjectCollection.AddRange documentation
for (int i = 0; i < 5; i++)
{
listBox3.Items.Add(Url[i]);
}
If you can ensure that there are always > 5 elements, this should be ok.
try this:
for(int i=0; i<5; ++i)
{
listBox3.Items.Add(Url[i]);
}
You can use GetRange
Url.GetRange(0,5);
listBox3.Items.AddRange(Url.GetRange(0,5));
With Start index and count..
List<string> fiveURLs = URL.GetRange(0, 5);
first five elements
for (int i = 0; i < Url.Count && i < 5; i++)
{
listBox3.Items.Add(Url[i]);
}
fifth element
listBox3.Items.Add(Url[4]);