C# 对目标的线性搜索

C# 对目标的线性搜索,c#,arrays,linear-search,C#,Arrays,Linear Search,在我的课程中,作为编码挑战的一部分,我们必须为10个不同的任务生成代码 在本任务中,我的目标是制作一个线性搜索算法,在数组中搜索特定项,并显示其位置(如果找到) 这是我当前的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Linearsearch2 { class Progra

在我的课程中,作为编码挑战的一部分,我们必须为10个不同的任务生成代码

在本任务中,我的目标是制作一个线性搜索算法,在数组中搜索特定项,并显示其位置(如果找到)

这是我当前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Linearsearch2
{
    class Program
    {
        static void Main(string[] args)
        {
            var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99 }; //Sets up the array

            var targetvalue = 77; //Establishes what number the search will attempt to find.
            var targetpos = -1; //Establishes the position in the array of the target.
            var targetnumber = 0; //Establishes the counter for the number of times the target appears.
            bool found = false; //Decides wether to change the number or use a counter method.

            var foundpositions = new int[] { }; //Establishes an array which will hold the positions of located items

            for (var i = 1; i < array.Length; i++)
            {
                if (found == true && array[i] == targetvalue)
                {
                    targetnumber = targetnumber + 1;
                }

                if (found == false && array[i] == targetvalue) //If the target value has not been found yet
                {
                    foundpositions.Add(i); //This is the line i need help with. I dont know how to add a value to an array properly.
                    found = true;
                }
            }

            if (targetpos != -1){ //If the target number was found
                Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome.
            }
            else //If the target number was not found
            {
                Console.WriteLine("The number " + targetvalue + " did not appear in this array."); // Prints the final outcome.
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间Linearsearch2
{
班级计划
{
静态void Main(字符串[]参数)
{
var array=newint[]{1,31,10,9,420,-5,77,420,300,99};//设置数组
var targetvalue=77;//确定搜索将尝试查找的数字。
var targetpos=-1;//建立目标数组中的位置。
var targetnumber=0;//建立目标出现次数的计数器。
bool found=false;//决定是更改数字还是使用计数器方法。
var foundpositions=new int[]{};//建立一个数组,该数组将保存已定位项的位置
for(var i=1;i
我需要帮助的问题是第31行,与 增加第(i)款

我不知道向数组正确添加值的行,这似乎是导致问题的原因。 (在此行中,我试图将搜索的当前位置添加到稍后显示的数组中)

谢谢你的帮助。此外,如果有任何其他明显的错误,指出他们将不胜感激

我需要帮助的问题是第31行 增加第(i)款

数组是动态的,并且没有
add()
方法。您可以使用
列表

替换此项:

var foundpositions = new int[] { };
为此:

var foundpositions = new List<int>();
因此,如果
块:

if (targetpos != -1){ //If the target number was found
      Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome.
}
在这个任务中,我的目标是制作一个线性搜索算法 在数组中搜索特定项,并显示其 位置(如果找到)

正如您的代码当前所示,似乎有几个错误。但是,下面的示例将帮助您开始:

public static int LinearSearch(int[] items, int target)
{
       if (items == null) throw new ArgumentNullException("argument items has null reference");
       if (items.Length == 0) return -1; // return -1 if the item is not found
       for (int i = 0; i < items.Length; i++)
            if (items[i] == target) return i;
       return -1; // return -1 if the item is not found
}
公共静态int-LinearSearch(int[]项,int目标)
{
如果(items==null)抛出新的ArgumentNullException(“参数项具有null引用”);
if(items.Length==0)返回-1;//如果找不到项,则返回-1
对于(int i=0;i
然后只需在
main
方法中调用
LinearSearch
,传入所需的数据,就可以了

不要忘记将
LinearSearch
返回的值分配到变量中,或者简单地将其打印到控制台

我需要帮助的问题是第31行 增加第(i)款

数组是动态的,并且没有
add()
方法。您可以使用
列表

替换此项:

var foundpositions = new int[] { };
为此:

var foundpositions = new List<int>();
因此,如果
块:

if (targetpos != -1){ //If the target number was found
      Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome.
}
在这个任务中,我的目标是制作一个线性搜索算法 在数组中搜索特定项,并显示其 位置(如果找到)

正如您的代码当前所示,似乎有几个错误。但是,下面的示例将帮助您开始:

public static int LinearSearch(int[] items, int target)
{
       if (items == null) throw new ArgumentNullException("argument items has null reference");
       if (items.Length == 0) return -1; // return -1 if the item is not found
       for (int i = 0; i < items.Length; i++)
            if (items[i] == target) return i;
       return -1; // return -1 if the item is not found
}
公共静态int-LinearSearch(int[]项,int目标)
{
如果(items==null)抛出新的ArgumentNullException(“参数项具有null引用”);
if(items.Length==0)返回-1;//如果找不到项,则返回-1
对于(int i=0;i
然后只需在
main
方法中调用
LinearSearch
,传入所需的数据,就可以了


不要忘记将
LinearSearch
返回的值赋给一个变量,或者只是将其打印到控制台。

我不确定您为什么要检查是否找到了目标数字。如果您想在数组中获取所有等于目标int的int的索引,那么您可以简单地在数组中循环,并将匹配项放入int列表中,然后返回此列表

此返回列表的计数将告诉您有多少匹配目标,并且该列表将包含这些匹配的索引。似乎没有任何理由检查在阵列中循环时是否找到目标。如果返回的列表为空,则未找到目标。下面的代码使用了这种方法。我希望我没有错过什么

private static void FindTargets() {
  var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99, 1, 31, 10, 9, 420, -5, 77, 420, 300, 99 }; //Sets up the array
  int target = 77;
  List<int> foundTargets = GetPositions(target, array);
  StringBuilder sb = new StringBuilder();
  sb.Append("There are " + foundTargets.Count + " int(s) that match " + target + Environment.NewLine);
  sb.Append("The array indexs for the target ints " + target + " are: ");
  int count = 0;
  foreach (int curInt in foundTargets) {
    sb.Append(curInt);
    if (count < foundTargets.Count - 1)
      sb.Append(", ");
    count++;
  }
  Console.WriteLine(sb.ToString());
}

private static List<int> GetPositions(int target, int[] intArray) {
  List<int> foundTargets = new List<int>();
  for (int i = 0; i < intArray.Length; i++) {
    if (intArray[i] == target) {
      foundTargets.Add(i);
    }
  }
  return foundTargets;
}
private static void FindTargets(){
var数组=新的int[]{1,31,10,9,420,-5,77,420,300,99,1,31,10,9,420-