需要帮助来查找数组范围内的值 [英] Need help on finding if a value it is in Array range
问题描述
我正在做一些关于Web服务的考试。
我创建了一个与数组有关的Web服务,比如找到非零值,正数,负数,两个给定值之间的成员等。
问题是我不知道如何找到给定值是否在给定数组的范围内。
这里是我在网络服务和服务的客户中制作的一些代码。
网络服务应用。
public class Service1:System.Web.Services.WebService
{
[WebMethod]
// 非零成员
public List< int> AnetaretJoZero( int [] anetaret)
{
var lista = new List< int>();
for ( int i = 0 ; i < anetaret.Length; i ++)
{
if (anetaret [i]!= 0 )
{
lista.Add(anetaret [i]);
}
}
return lista;
}
[WebMethod]
// 否定成员
public List< int> AnetaretNegativ( int [] negativ)
{
var list_negativ = new List< int>();
for ( int i = 0 ; i < negativ.Length; i ++)
{
if (negativ [i] < 0 )
{
list_negativ.Add(是负面的[I]);
}
}
return list_negativ;
}
[WebMethod]
// 正面会员
public 列表< int> AnetaretPozitiv( int [] pozitiv)
{
var list_pozitiv = new List< int>();
for ( int i = 0 ; i < pozitiv.Length; i ++)
{
if (pozitiv [i] > 0 )
{
list_pozitiv.Add( pozitiv [I]);
}
}
return list_pozitiv;
}
[WebMethod]
// 两个给定值之间的成员
public List< int> AnetaretMesVlerave( int [] mesvlera, int a, int b)
{
var list_mesvlera = new List< int> ();
for ( int i = 0 ; i < mesvlera.Length; i ++)
{
if (mesvlera [i] > = a& mesvlera [i] < = b)
{
list_mesvlera.Add(mesvlera [i]);
}
}
return list_mesvlera;
}
// 这里我正在尝试做一些关于我的问题的代码
// 查找给定值是否在给定数组的范围内
}
消费者代码应用
static void Main( string [] args )
{
// 这是一个消费者数组
< span class =code-keyword> int [] vek = {-3, 1 , 0 , 2 , - 5,-25, 0 , 5 , 6 , 12 , 45 , 0 };
referenca.Service1 sherbimi = new referenca.Service1();
// vek = sherbimi.Vektori(vek);
int [] resVek = sherbimi.AnetaretJoZero(vek);
// 数组的非零值
控制台。 WriteLine( Anetaret jozerotëvektoritjanë:);
Console.WriteLine();
for ( int i = 0 ; i < resVek.Length; i ++)
{
Console.Write(resVek [i] + ,);
}
// 数组的负面成员
int [] vekrez = sherbimi.AnetaretNegativ(vek);
Console.WriteLine();
Console.WriteLine( Anetaretnegativtëvektoritjanë:);
Console.WriteLine();
for ( int j = 0 ; j < vekrez.Length; j ++)
{
Console.Write(vekrez [j] + ,);
}
// 数组正面成员
int [] vekpoz = sherbimi.AnetaretPozitiv(vek);
Console.WriteLine();
Console.WriteLine( Anetaret pozitiv jane:);
Console.WriteLine();
for ( int i = 0 ; i < vekpoz.Length; i ++)
{
Console.Write(vekpoz [i] + ,);
}
// 两个给定值之间的成员
int [] vekmesvlera = sherbimi.AnetaretMesVlerave(vek,-5, 12 );
Console.WriteLine();
Console.WriteLine( Anetaret mesvlerave jane:);
Console.WriteLine();
for ( int i = 0 ; i < vekmesvlera.Length; i ++)
{
Console.Write(vekmesvlera [i] + ,);
}
// 这就是我要查找的内容
// 如果值存在于数组范围内
// 此方法检查数组中是否存在给定值
< span class =code-keyword> int [] ekvlera = sherbimi.EkzistonVlera(vek,-5);
Console.WriteLine();
Console.WriteLine( AnetariidhënëY:);
Console.WriteLine();
if (((IList< int>)ekvlera).Contains(-5)))
{
Console.Write( Vlera ekziston);
}
else
Console.WriteLine( Vleraedhënënukekziston);
Console.ReadLine();
}
提前感谢您的回复。
一本书的一位作者(对不起,我现在不能完全准确地提及),合理地指出:'范围'是一个坏词!。 (允许值)的集合称为域。有两个与数组相关的域:一个是其索引的域,通常称为范围,以及元素值的域。
如果我只看到了标题,我会决定你在谈论指数。然后你可以说一些整数索引
值是否在域中,这只是index> = 0&& index< = vec.length - 1
。这几乎总是如此,但并非总是如此。您可以使用类System.Array
创建不是从零开始的数组,您需要做一些更复杂的检查:
< pre lang =c#> bool isInRange = index > = ver.GetLowerBound( 0 )&& index < = vec.GetUpperBound( 0 );
请参阅:
https://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library /system.array.getlowerbound%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.array.getupperbound%28v=vs.110 %29.aspx [ ^ ]。
要检查某个值是否是数组值的一个元素,您可以使用foreach
循环并比较遍历所有数组元素的函数使用'=='运算符的等价值。如果找到匹配元素,请不要忘记立即返回。这太难以解释了。但您不必这样做,因为您可以使用此方法: https://msdn.microsoft.com/en-us/library/eha9t187%28v=vs.110%29.aspx [ ^ ]。
无论如何你都可以使用基于零的数组(这是你的数组的情况),但一般情况下,不是与其他数组一起使用,因为未找到用-1表示。
-SA
I'm doing some exams about web services.
I create a web service that it has to do with arrays, like finding non zero values, positive, negative, members between two given values etc.
The problem is that I don't know how to find if a given value is in range of a given array.
Here is some code I made in web service and in a costumer of the service.
Web service app.
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
//non zero members
public List<int> AnetaretJoZero(int [] anetaret)
{
var lista = new List<int>();
for (int i = 0; i < anetaret.Length; i++)
{
if (anetaret[i] != 0)
{
lista.Add(anetaret[i]);
}
}
return lista;
}
[WebMethod]
//negative members
public List<int> AnetaretNegativ(int[] negativ)
{
var list_negativ = new List<int>();
for (int i = 0; i < negativ.Length; i++)
{
if (negativ[i] < 0)
{
list_negativ.Add(negativ[i]);
}
}
return list_negativ;
}
[WebMethod]
//positive members
public List<int> AnetaretPozitiv(int[] pozitiv)
{
var list_pozitiv = new List<int>();
for (int i = 0; i < pozitiv.Length; i++)
{
if (pozitiv[i] > 0)
{
list_pozitiv.Add(pozitiv[i]);
}
}
return list_pozitiv;
}
[WebMethod]
//members between two given values
public List<int> AnetaretMesVlerave(int[] mesvlera, int a, int b)
{
var list_mesvlera = new List<int>();
for (int i = 0; i < mesvlera.Length; i++)
{
if (mesvlera[i]>=a & mesvlera[i]<=b)
{
list_mesvlera.Add(mesvlera[i]);
}
}
return list_mesvlera;
}
// here I'm attempting to do some code about my problem to
//find if the given value is in range of a given array
}
The consumer code app
static void Main(string[] args)
{
//this is a consumer array
int[] vek = { -3, 1, 0, 2,-5, -25, 0, 5, 6, 12, 45, 0 };
referenca.Service1 sherbimi = new referenca.Service1();
//vek = sherbimi.Vektori(vek);
int[] resVek = sherbimi.AnetaretJoZero(vek);
//non zero values of the array
Console.WriteLine("Anetaret jo zero të vektorit janë: ");
Console.WriteLine();
for (int i = 0; i < resVek.Length; i++)
{
Console.Write(resVek[i] + ", ");
}
//negative member of array
int[] vekrez = sherbimi.AnetaretNegativ(vek);
Console.WriteLine();
Console.WriteLine("Anetaret negativ të vektorit janë: ");
Console.WriteLine();
for (int j = 0; j < vekrez.Length; j++)
{
Console.Write(vekrez[j] + ", ");
}
//positive member of array
int[] vekpoz = sherbimi.AnetaretPozitiv(vek);
Console.WriteLine();
Console.WriteLine("Anetaret pozitiv jane: ");
Console.WriteLine();
for (int i = 0; i < vekpoz.Length; i++)
{
Console.Write(vekpoz[i] + ", ");
}
//members between two given values
int[] vekmesvlera = sherbimi.AnetaretMesVlerave(vek, -5, 12);
Console.WriteLine();
Console.WriteLine("Anetaret mesvlerave jane: ");
Console.WriteLine();
for (int i = 0; i < vekmesvlera.Length; i++)
{
Console.Write(vekmesvlera[i] + ", ");
}
//this is what I'm trying to do to find
//if the value exists in the range of the array
// this method checks if the given value exists in the array or not
int[] ekvlera = sherbimi.EkzistonVlera(vek, -5);
Console.WriteLine();
Console.WriteLine("Anetari i dhënë Y: ");
Console.WriteLine();
if (((IList<int>)ekvlera).Contains(-5)))
{
Console.Write("Vlera ekziston");
}
else
Console.WriteLine("Vlera e dhënë nuk ekziston");
Console.ReadLine();
}
Thank you in advance for your reply.
One author of one book (sorry, I cannot give an exact quite and reference at this time), reasonably noted: "'range' is a bad term!". The set of (allowed values) is called "domain". There are two domains related to the arrays: one is the domain of its indices, most usually called 'range', and the domain of values of the elements.
If I saw only the title, I would have decided that you are talking about indices. Then you could have said someinteger index
value is in domain or not, this is simplyindex >= 0 && index <= vec.length - 1
. This is almost always so, but not always. You could create not a zero-based array using the classSystem.Array
, the you would need to do a bit more complicated check:
bool isInRange = index >= ver.GetLowerBound(0) && index <= vec.GetUpperBound(0);
Please see:
https://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.array.getlowerbound%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.array.getupperbound%28v=vs.110%29.aspx[^].
To check is some value is an element of the set of array values, you can write a function traversing all array element withforeach
loop and compare the value for equivalence using '==' operator. Don't forget to return immediately if the matching element is found. This is too trivial to explain. But you don't have to do it, because you can use this method: https://msdn.microsoft.com/en-us/library/eha9t187%28v=vs.110%29.aspx[^].
You can use it with zero-based arrays anyway (which is the case with your arrays), but, generally, not with others, because "not found" is indicated by −1.
—SA
这篇关于需要帮助来查找数组范围内的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!