通过引用传递ArrayList

时间:2014-06-08 17:38:14

标签: c# arrays arraylist reference

所以我有另一个任务,我正在尝试使用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个构建错误...我认为这与第一个“函数”中的代码有关。

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

有什么想法吗?

编辑:根据我的建议改变了我的代码。仍然收到“未知”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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

查看您的方法定义:

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 - 通用集合为您提供类型安全性。在我们使用泛型之前,{1.1}是.NET 1.1的延续。作为参考,.NET Framework的当前版本是4.5.1。自.NET 2.0发布以来,我们已经有了仿制药,这种仿制药大约在十年前推出。