【问题标题】:passing an ArrayList by reference [closed]通过引用传递 ArrayList [关闭]
【发布时间】:2014-06-08 17:38:13
【问题描述】:

所以我有另一个任务,我正在尝试使用 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();
    }
}
}

【问题讨论】:

  • 你应该使用List&lt;T&gt;,而不是ArrayList
  • 错误很明显。您的代码有多个冗余元素,请删除它们。从int GetLastNames(ArrayList lastNames, ref string exitValue) 中的第二个参数开始。它也不需要返回值。
  • @Slaks:你看过练习描述了吗?
  • 错误信息的哪一部分你不明白?
  • @HenkHolterman:如果他的练习告诉他使用非泛型集合,他应该改用更好的课程。

标签: c# arrays arraylist reference


【解决方案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&lt;string&gt;) 而不是 ArrayList——泛型集合为您提供类型安全。 ArrayList 是 .NET 1.1 天的遗留物,在我们有泛型之前。作为参考,.NET Framework 的当前版本是 4.5.1。自 .NET 2.0 以来,我们就已经有了泛型,它大约在十年前问世。

【讨论】:

  • 不,exitValue 不需要值。它需要一个变量作为参数。或者可以完全删除。
  • @HenkHolterman 感谢您的更正——在这种情况下,我错误地使用了“价值”一词。我已经相应地更新了我的答案。
  • 看起来不错。我现在唯一的问题是主方法中没有识别“lastNames”。我很确定在这方面我只是一个菜鸟(很明显)。但是,您能否详细说明一下 List 信息?
  • @TheNoobofNoobs 当您说“它没有构建”时,提供它没有构建的原因对于帮助您排除故障很重要。我猜问题的根源是ArrayList lastNames = new ArrayList(); 行。您正在声明 ArrayList,然后尝试稍后重新声明它。
  • 此时这是一个不同的问题,可能需要一个新问题。问题出在Console.WriteLine(lastNames)。想想为什么这是一个问题,如果你无法弄清楚,请发布一个新问题。当然,我建议先对该主题进行一些研究。这里有一个提示可以帮助您进行调试:您正在尝试打印列表中包含的项目,但您将整个列表传递给 Console.WriteLine
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2012-10-15
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2013-09-05
相关资源
最近更新 更多