C# 通过引用传递ArrayList
所以我有另一个赋值,我试图使用ArrayList对象来编译一个姓氏列表,获得一个计数,按升序排序,然后按降序排序。我遇到的问题是,VisualStudio说在我进行编译/调试时出现了一个错误,但没有任何标记,我似乎无法找出问题所在C# 通过引用传递ArrayList,c#,arrays,arraylist,reference,C#,Arrays,Arraylist,Reference,所以我有另一个赋值,我试图使用ArrayList对象来编译一个姓氏列表,获得一个计数,按升序排序,然后按降序排序。我遇到的问题是,VisualStudio说在我进行编译/调试时出现了一个错误,但没有任何标记,我似乎无法找出问题所在 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统集合;
名称空间Lab5_exB
{
班级计划
{
静态void Main(字符串[]参数)
{
串另一个列表;
做
{
ArrayList lastNames=新的ArrayList();
字符串存在值;
做
{
Console.WriteLine(“输入姓氏…”);
exitValue=Console.ReadLine();
if(exitValue==“N”| | exitValue==“N”)
{
打破
}
lastNames.Add(exitValue);
}而(exitValue!=“N”| exitValue!=“N”);
Console.WriteLine(“输入的姓氏数量:“+lastNames.Count”);
lastNames.Sort();
Console.WriteLine(“按字母升序排列的姓氏”);
Console.WriteLine(“------------------------------------------------”);
int i=0;
而(i
我已经使用单独的函数编写了代码,并将其全部拼凑成一个混乱的方法/函数。上面是一团糟。以下是单独的功能:
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统集合;
名称空间Lab5_exB
{
班级计划
{
公共静态int-GetLastNames(ArrayList-lastNames,引用字符串exitValue)
{
做
{
Console.WriteLine(“输入姓氏…”);
exitValue=Console.ReadLine();
if(exitValue==“N”| | exitValue==“N”)
{
打破
}
lastNames.Add(exitValue);
}而(exitValue!=“N”| exitValue!=“N”);
返回0;
}
公共静态整型显示ArrayNames(ArrayList lastNames)
{
Console.WriteLine(“输入的姓氏数量:“+lastNames.Count”);
lastNames.Sort();
Console.WriteLine(“按字母升序排列的姓氏”);
Console.WriteLine(“------------------------------------------------”);
int i=0;
而(i
当使用不同的功能时。我收到一个错误,“没有方法'GetLastNames'的重载接受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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统集合;
名称空间Lab5_exB
{
班级计划
{
公共静态ArrayList GetLastNames()
{
字符串存在值;
var lastNames=new ArrayList();
做
{
Console.WriteLine(“是否要输入其他名称?(Y/N)”);
exitValue=Convert.ToString(Console.Read());
if(exitValue==“N”| | exitValue==“N”)
{
打破
}
Console.WriteLine(“输入姓氏…”);
Add(Console.ReadLine());
}而(exitValue!=“N”| exitValue!=“N”);
返回姓氏;
}
公共静态无效显示ArrayNames(ArrayList lastNames)
{
Console.WriteLine(“输入的姓氏数量:“+lastNames.Count”);
lastNames.Sort();
控制台。WriteLine(“Las
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;
}