c#如何在arraylist中打印字符串的字符

本文关键字:打印 字符串 字符 arraylist | 更新日期: 2023-09-27 18:13:41

这个程序打印字符串输入的所有可能性,例如,如果输入是abc',输出应该是组合,这很有效,但当我创建数组列表时,它打印的是数字而不是字符串组合,代码:

string input = Console.ReadLine();
int sl = input.Length;
ArrayList arr = new ArrayList();
for (int three = 0; three < sl; three++) {
    for (int two = 0; two < sl; two++) {
        for (int one = 0; one < sl; one++) {
            char onef = input[one];
            char twof = input[two];
            char threef = input[three];

            arr.Add(threef + twof + onef);
            Console.Write(threef);
            Console.Write(twof);
            Console.WriteLine(onef);
        }
    }
}
Console.WriteLine("The elements of the ArrayList are:");
foreach(object obj in arr) {
    Console.WriteLine(obj);
}

arraylist的输出是数字,而不是字符串字符,救命!

c#如何在arraylist中打印字符串的字符

只需更改这三行

发件人:

 char onef = input[one];
 char twof = input[two];
 char threef = input[three];

收件人:

  string onef = input[one].ToString();
  string twof = input[two].ToString();
  string threef = input[three].ToString();

替换:更改一行:

arr.Add(string.Format("{0}{1}{2}", a, b, c));

char不能像string那样级联。对+进行了数值解释。