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]);
Related
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 needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.
for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]
Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}
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
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
Can anyone give me an example of how to build a function in C# that returns a delimited set of string numbers?
If anyone could help that would be amazing. Thanks.
EDIT: Now answered. Thanks T.S. for the help.
Along the lines of what I was going for with:
public string GetDelimited(int noOfLoops, string delimiter)
{
var build = new StringBuilder();
for (int j=1; j< noOfLoops + 1; j++)
{
if (j > 1) builder.Append(delimiter)
}
return build.ToString();
}
This is example in the purest, basic, sugar-less form
public string GetDelimited(int numberOfLoops, string delimiter)
{
var builder = new StringBuilder();
for (int i= 1; i < numberOfLoops + 1; i++)
{
if (i > 1) builder.Append(delimiter);
builder.Append(i.ToString());
}
return builder.ToString();
}
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 9 years ago.
Improve this question
How to sort array without using sort method in C#
Visualization and Comparison of sorting algorithms in C#
example code
public IList BubbleSort(IList arrayToSort)
{
int n = arrayToSort.Count - 1;
for (int i = 0; i < n; i++)
{
for (int j = n; j > i; j--)
{
if (((IComparable)arrayToSort[j - 1]).CompareTo(arrayToSort[j]) > 0)
{
object temp = arrayToSort[j - 1];
arrayToSort[j - 1] = arrayToSort[j];
arrayToSort[j] = temp;
}
}
}
return arrayToSort;
}
above will surely helps you to understand the thing you want
By assigning value by value in alphabetical or whatever order.
Sorry, but I don't get your question. What is the actual problem you are trying to solve?
Use nested loop sorting methods such as bubble sort.
But why would you want to do such a thing?
Since the array implements IEnumerable<T>, you can use the OrderBy extention method on IEnumerable to sort it.
If your question is how to do it without using any built in functionality in the framework, I guess I will have to ask. Why??