通过引用传递数组列表

本文关键字:数组 列表 引用 | 更新日期: 2023-09-27 18:07:22

所以我有另一个赋值,我试图使用ArrayList对象来编译姓氏列表,得到一个计数,按升序排序,然后按降序排序。我遇到的问题是,Visual Studio说当我去编译/调试时有一个错误,但没有任何标记,我似乎无法找出问题所在。

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Collections;
   namespace Lab5_exB
   {
class Program
{
    static void Main(string[] args)
    {
        string anotherList;
        do
        {
            ArrayList lastNames = new ArrayList();
            string exitValue;
            do
            {
                Console.WriteLine("Enter a last name..");
                exitValue = Console.ReadLine();
                if (exitValue == "N" || exitValue == "n")
                {
                    break;
                }
                lastNames.Add(exitValue);
            } while (exitValue != "N" || exitValue != "n");
            Console.WriteLine("Amount of last names entered: " + lastNames.Count);
            lastNames.Sort();
            Console.WriteLine("Last names in Ascending Alphabetical Order");
            Console.WriteLine("------------------------------------------");
            int i = 0;
            while (i < lastNames.Count)
            {
                Console.WriteLine(lastNames);
                i++;
            }
            lastNames.Reverse();
            Console.WriteLine("Last names in Descending Alphabetical Order");
            Console.WriteLine("-------------------------------------------");
            int z = 0;
            while (z < lastNames.Count)
            {
                Console.WriteLine(lastNames);
                z++;
            }
            Console.WriteLine("Would you like to enter another list? (Y/N)");
            anotherList = Convert.ToString(Console.Read());
        }while (anotherList == "Y" || anotherList == "y");
        Console.Read();
    }
}
}

我已经写了我的代码使用单独的函数,并把它全部拍打成一个混乱的一个方法/函数混乱。上面是一团乱。下面是单独的函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Lab5_exB
{
class Program
{
    public static int GetLastNames(ArrayList lastNames, ref string exitValue)
    {
        do
        {
            Console.WriteLine("Enter a last name..");
            exitValue = Console.ReadLine();
            if (exitValue == "N" || exitValue == "n")
            {
                break;
            }
            lastNames.Add(exitValue);
        } while (exitValue != "N" || exitValue != "n");
        return 0;
    }
    public static int DisplayArrayNames(ArrayList lastNames)
    {
        Console.WriteLine("Amount of last names entered: " + lastNames.Count);
        lastNames.Sort();
        Console.WriteLine("Last names in Ascending Alphabetical Order");
        Console.WriteLine("------------------------------------------");
        int i = 0; 
        while (i < lastNames.Count)
        {
            Console.WriteLine(lastNames);
            i++;
        }
        return 0;
    }
    public static int ReverseArrayNames(ArrayList lastNames)
    {
        lastNames.Sort();
        lastNames.Reverse();
        Console.WriteLine("Last names in Descending Alphabetical Order");
        Console.WriteLine("-------------------------------------------");
        int z = 0;
        while (z < lastNames.Count)
        {
            Console.WriteLine(lastNames);
            z++;
        }
        return 0;
    }
    static void Main(string[] args)
    {
        string anotherList;
        do
        {
            ArrayList lastNames = new ArrayList();
            string exitValue;
            GetLastNames(lastNames);
            DisplayArrayNames(lastNames);
            ReverseArrayNames(lastNames);
            Console.WriteLine("Would you like to enter another list? (Y/N)");
            anotherList = Convert.ToString(Console.Read());
        }while (anotherList == "Y" || anotherList == "y");
        Console.Read();
    }
}
}

使用不同功能时。我收到一个错误,"没有重载方法'GetLastNames'需要1个参数",我没有看到问题……看起来写得很好。当写为一个方法/函数时,没有显示错误,但有1个构建错误…我认为这与第一个"函数"中的代码有关。

我想也许声明的函数需要设置为字符串,但他们正在标记,因为他们没有返回值,我不认为我可以返回一个数组列表。

任何想法?

编辑:根据别人的建议修改了我的代码。在构建中仍然收到"unknown" 1失败
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Lab5_exB
{
class Program
{
    public static ArrayList GetLastNames()
    {
        string exitValue;
        var lastNames = new ArrayList();
        do
        {
            Console.WriteLine("Would you like to enter another name? (Y/N)");
            exitValue = Convert.ToString(Console.Read());
            if (exitValue == "N" || exitValue == "n")
            {
                break;
            }
            Console.WriteLine("Enter a last name..");
            lastNames.Add(Console.ReadLine());
        } while (exitValue != "N" || exitValue != "n");
        return lastNames;
    }
    public static void DisplayArrayNames(ArrayList lastNames)
    {
        Console.WriteLine("Amount of last names entered: " + lastNames.Count);
        lastNames.Sort();
        Console.WriteLine("Last names in Ascending Alphabetical Order");
        Console.WriteLine("------------------------------------------");
        int i = 0; 
        while (i < lastNames.Count)
        {
            Console.WriteLine(lastNames);
            i++;
        }
    }
    public static void ReverseArrayNames(ArrayList lastNames)
    {
        lastNames.Sort();
        lastNames.Reverse();
        Console.WriteLine("Last names in Descending Alphabetical Order");
        Console.WriteLine("-------------------------------------------");
        int z = 0;
        while (z < lastNames.Count)
        {
            Console.WriteLine(lastNames);
            z++;
        }
    }
    static void Main(string[] args)
    {
        string anotherList;
        do
        {
            var lastNames = GetLastNames();
            DisplayArrayNames(lastNames);
            ReverseArrayNames(lastNames);
            Console.WriteLine("Would you like to enter another list? (Y/N)");
            anotherList = Convert.ToString(Console.Read());
        }while (anotherList == "Y" || anotherList == "y");
        Console.Read();
    }
}
}

通过引用传递数组列表

看看你的方法定义:

public static int GetLastNames(ArrayList lastNames, ref string exitValue)

接受两个参数。你试着用GetLastNames(lastNames)来调用它。您需要在调用exitValue时为该方法提供一个变量。

这完全偏离了实际的逻辑。

试试这个:

public static ArrayList GetLastNames()
{
    var lastNames = new ArrayList();
    do
    {
        Console.WriteLine("Enter a last name..");
        exitValue = Console.ReadLine();
        if (exitValue == "N" || exitValue == "n")
        {
            break;
        }
        lastNames.Add(exitValue);
    } while (exitValue != "N" || exitValue != "n");
    return lastNames;
}

则仅用GetLastNames()调用。您需要将结果赋值给一个变量——您可以这样做:

var lastNames = GetLastNames();

这将获取GetLastNames方法执行期间创建的对象,并将其赋值给Main方法中的一个变量。

也就是说,您应该使用泛型集合(List<string>)而不是ArrayList——泛型集合提供类型安全。ArrayList是。net 1.1的遗留版本,在我们有泛型之前。作为参考,. net框架的当前版本是4.5.1。从。net 2.0开始,我们就有了泛型,它差不多是在十年前出现的。