【问题标题】:Changing the index of arrays更改数组的索引
【发布时间】:2021-06-28 11:14:12
【问题描述】:

我被以下问题困住了。我正在尝试创建一个具有预先指定大小和元素的数组。稍后我要求用户向我提供他们想要添加的名称。然后我创建一个大一号的新数组,并在最后一个位置添加新的名称。后来我要求用户从他们希望删除的数组中为我提供一个名称。这就是问题出现的地方。我知道我可以使用 Array-Copy 等方法,但我希望只能通过 index.html 来解决这个问题。这是我目前写的代码:

class Program
{
    static void Main(string[] args)
    {
        string[] names = { "Adam", "Eva" };
        foreach (var item in names)
        {
            Console.WriteLine(item);
        }

        Console.Write("Skriv in ett namn du vill lägga till: ");
        string newAdd = Console.ReadLine();

        string[] addedNames = AddName(names, newAdd);
        foreach (var item in addedNames)
        {
            Console.WriteLine(item);
        }

        Console.Write("Ange det element du vill ta bort: ");
        string tabort = Console.ReadLine();
        string[] removedNames = RemoveName(addedNames, tabort);

        foreach (var item in removedNames)
        {
            Console.WriteLine(item);
        }
    }
    static string[] AddName(string[] names, string newAdd)
    {
        string[] newNames = new string[names.Length + 1];
        for (int i = 0; i < names.Length; i++)
        {
            newNames[i] = names[i];
        }
        newNames[newNames.Length - 1] = newAdd;
        return newNames;
    }

    static string[] RemoveName(string[] addedNames, string tabort)
    {
        string lLName = tabort.ToLower();
        string[] newnames = new string[addedNames.Length - 1];

        if (lLName == addedNames[0].ToLower())
        {
            int index = 0;
            for (int i = 1; i < addedNames.Length; i++)
            {
                newnames[index] = addedNames[i];
                index++;
            }
        }
        else if (lLName == addedNames[addedNames.Length-1].ToLower())
        {
            for (int i = 0; i < addedNames.Length-1; i++)
            {
                newnames[i] = addedNames[i];
            }
        }
        else if (lLName != addedNames[addedNames.Length-1].ToLower() && lLName != addedNames[0].ToLower())
        {
            for (int i = 1; i < addedNames.Length; i++)
            {

            }
        }

        return newnames;
    }
}

}

所以你可以看到我正在尝试实现最后一个 else if 语句,但是当用户提供的名称出现在我的 addedNames 数组中的某个位置时,我不确定如何处理,除了第一个和最后一个位置。

如果您认为有一种更简单的方法来处理该问题,而不是为每种情况使用近 4 个 if 语句,我也会很高兴。在我看来,如果一个人设法实现最后一个 if 语句,那么无论用户提供的要删除的名称位于 addedNames 数组中的哪个位置,它都会起作用。

【问题讨论】:

  • 为什么不使用列表?已被创建为“包含电池”的对象,用于处理您的问题......
  • 练习应该是用数组来完成的:)
  • 运动?那么,我们不应该给出提示吗?无论如何,从IndexOf 函数开始,它可以为您提供数组中元素的位置。我想你离目标只有一步之遥:docs.microsoft.com/en-us/dotnet/api/…
  • 锻炼自己,不在学校。
  • 我想在没有任何帮助功能的情况下完成它。只是索引。你觉得有办法通过索引解决吗?

标签: c# arrays indexing


【解决方案1】:

为什么不直接找到需要替换的名字的索引(tabort),直接把值改成newAdd?

string newAdd = Console.ReadLine();
string tabort = Console.ReadLine();
int i = Array.FindIndex(names, x => x.Contains(tabort));
names[i] = newAdd; 

【讨论】:

  • 你能解释一下你的意思吗?你的意思是我应该问用户他们想要删除哪个元素并要求他们提供它的索引然后删除那个项目?如果是,那么我就这样做了,这很容易。我想尝试通过要求用户向我提供他们想要从数组中删除的元素的名称来解决这个问题。我意识到这是一个艰难的过程。
猜你喜欢
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
相关资源
最近更新 更多