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 have an array and am trying to extract the vlaues. I have tried to find the length of the array, but as the array isnt always full the .length doesnt work for me. How do i work out how many values are stored in the array?
Heres the code;
int length = numbers.Length;
I have created the array in the main section of the code, I am trying to make a function that can get the values out of the code using a for loop. E.G
int number(i) = numbers[i]
so that the variable number[i] will become number0, and then assigned the value in the first row of the array
If you want to use array which is not always full, then you definitely should use List<T> instead of array, because list can contain variable number of items. With array you never can tell if default value (zero for integer) was assigned to array item, or item was not assigned at all. Getting list items count will look like list.Count.
Of course, you can get number of array items which have non-default value (but see above why it might be not good approach):
int[] array = { 1, 2, 0, 1, 5 };
int count = array.Count(i => i != default(int)); // 4
try foreach you do not need the length. Here is code:
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
foreach (int element in numbers)
{
System.Console.WriteLine(element);
}
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 8 months ago.
Improve this question
So I tried to return my array with a foreach loop... but no results... If someone can help me return an array, it will help me greatly.
Here is the test code of how I tried to do that:
int[] ints = { 1, 2, 3 };
foreach(int i in ints)
{
return ints[i];
}
The code in the question shows how javascript handles foreach loops. In javascript, the foreach loop iterates over the indexes. C# is different. In C#, the foreach loop iterates over the values. Therefore you want this:
int[] ints = { 4, 5, 6 };
foreach(int i in ints)
{
return i;
}
Additionally, this code will exit the method as soon as it hits the return keyword for the first time and only provide the first value. If you want to return an array, you can just... return the array:
int[] ints = { 4, 5, 6 };
return ints;
Like #lee Taylor mentioned. It is weird to do this, but yield is what you want.
int[] ints = { 1, 2, 3 };
foreach(int i in ints)
{
yield return i;
}
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 1 year ago.
Improve this question
I need to substract given sum from array of elements. I.e. if given value is 15 and array starts with [10, 20,...] I need to substract 10 from the first elements (resulting with 0 for it) and the rest continue to subsequent elements - so second would be decreased by 5 (15 - 10), and the rest stay untouched.
int[] myNum = { 10, 20, 30, 40 };
if given value is 15 then I need to subtract value from the array. The new array will be
int[] myNum=[0,5,30,40]
I can easily create new array with all elements of the array decreased by the given number with basic for loop, but I don't know how to change that number based on how much I already substracted.
Your best bet is a for loop.
This will allow you to perform an action (I.E subtraction) on each 'element' of an array.
By using a return statement, you can return the new array that has had each element modified
EDIT As per #AlexeiLevenkov's comment, I have updated my answer to keep a count of the remaining subtraction.
Using this to test :
using System;
public class Program
{
public static void Main()
{
int[] array = new int[]{5,10,15,20,25,30,35};
array=SubtractArray(array,25);
Console.WriteLine("Output is:");
foreach(int v in array){
Console.WriteLine(v+", ");
}
}
public static int[] SubtractArray(int[] array , int subtraction){
for(int i=0; i< array.Length;i++){
if(subtraction>0){
int newValue=array[i]-subtraction;
if(newValue<1){
newValue=0;
subtraction=subtraction-array[i];
}
array[i]=newValue;
}
}
return array;
}
}
I don't know wath you said but I think it that :
The Arrays work 0,1,2,3,4,5,6
if I made a arrays like that
int[] _int = {10, 50, 30};
for go to the fist (10) and substract I need to do that
Sorry for my english I'm french
_int[0] -= 10;
becose it will select automatic the first (0)
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 7 years ago.
Improve this question
I need to count the number of integers between two sorted sets to determine if a lotto ticket is a winner. One sorted set being winningNumbers and the other ticketNumbers. I was told there is a intersect function but I cannot find a function to achieve the required results. Ideally I would like a function that returns an int representing the number of common integers between the sets.
Even if it's not sorted this works:
IEnumerable<int> winning = winningNumbers.Intersect(ticketNumbers);
int countOfWinningNumbers = winning.Count();
If you want to process it further it would be better to create a collection:
List<int> winningList = winning.ToList();
int countOfWinningNumbers = winningList.Count;
Since you've asked explicitly for a SortedSet approach, using SortedSet.IntersectWith might be more efficient(O(n)) but modifies the source set:
SortedSet<int> winningNumbers = new SortedSet<int> { 2, 3, 7 };
SortedSet<int> ticketNumbers = new SortedSet<int> { 1, 2, 3, 4, 5 };
ticketNumbers.IntersectWith(winningNumbers); // now ticketNumbers contains only 2 and 3
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 7 years ago.
Improve this question
I'm beginner in c#,i want to declare this c# array:
double[] array1;
for(int i=0;i<10;i++)
array1[i]=i;
but get error and c# say change that array to this:
double[] array1={1.1,2.2};
but i have to use my first code,how can i solve that?thanks.
double[] array1; only defines the variable, but doesn't instantiate the array.
Do this:
double[] array1 = new double[10];
for(int i=0;i<10;i++)
array1[i]=i;
In .Net when you initialise an array, you need to provide a size so that memory can be allocated to it.
this is done as such
double[] array1 = new double[10];
It is also worth noting that Arrays are statically sized so once you create it with that size, if you want it changed you need to create a new array.
you can try this :
int a = 10; // the size you want
array1 = new double[a];
for (int i = 0; i < a; i++)
array1[i] = i;
You simply cannot use your first code because it's not allowed in C#.
An array needs to know its length when created, and that's why you need to specify its length when creating it.
Also, you must assign it because otherwise, array1 remains null, which means you'll get an error when trying to use it.
So you have two choices here:
double[] array1 = new double[10], which means "create an array with room for 10 elements and set each value to 0.0", or you say
double[] array1 = { 1.0, 2.0, ..., 10.0 };, which sets the length of the array to the number of the elements you specified, and its values to the ones you specified (1.0-10.0 in the example).
If you know exactly number of elements in array you should use arrays. If you're uncertain about that use collection List and cast it to array (or just use it as it is).
Array:
double[] array1 = new double[10];
for(int i=0;i<10;i++) array1[i]=i;
Collection (don't use it if you know length of your array) :
list<double> coll1 = new List<double>();
for(int i=0; i < unknownNumber; i++) coll1.Add(i);
double[] array1 = coll1.ToArray();
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 have two String Arrays named as items and items1
items array consists of 1296 elements
items1 array consists of 8 elements
copy items1 into items. i tried something like this but it doesn't work, let check this out
items1.CopyTo(items, items1.Length -1);
Array.Copy(items1, items, items1.Length-1);
This should work
Array.Resize(ref items, items.Length + items1.Length);
Array.Copy(items1, 0, items, items.Length - items1.Length, items1.Length);
If you want elements to be appended rather than overwritten try the following
items = items.Concat(items1).ToArray();
Btw use meaningful names, items and items1 doesn't makes any sense
use Linq's Concat method
items.Concat(items1)
this will concatenate two arrays together and adding items1 at the end of items , i hope you want items array like that only.
It should work for you :
var items = new string[]{"A"};
var items1 = new string[] { "B" };
var res = new List<string>();
res.AddRange(items);
res.AddRange(items1);
items = res.ToArray();
The main problem in your case was increasing the items array length at run time. If you had sufficient length in items array then you could use:
Array.Copy(items1, 0, items, items.Length, items1.Length);
This is an simple example for copying an string Array.
string[] SourceArray= { "A", "B", "C", "D", "E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S" };
string[] DestArray= new string[8];
Array.Copy(SourceArray, 11, DestArray, 0, 8);
SourceArray=Input Array
11=starting index (From where the copy should start in source array)
DestArray=Is your array in which the elements have to be copied
0=Starting index of Destination Array
8=Number of elemets to be copied in the array
Output:
{L,M,N,O,P,Q,R,S}
This should do it:
Array.Resize(ref items, items.Length + items1.Length);
Array.Copy(items1, 0, items, items.Length-items1.Length, items1.Length);
It resizes the destination array, items, to be large enough for both arrays.
Then it copies the source array, items1, to the newly-added space at the end of the destination array.