简体   繁体   English

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

[英]c# How to print the chars of strings in arraylist

This program prints all possibilities of a string input, for example, if input is abc', the output should be the combinations, this works but when im creating the arraylist it's printing out numbers instead of the string combinations, code: 该程序将打印所有可能的字符串输入,例如,如果输入为abc',则输出应为组合,这是可行的,但是当我创建arraylist时,它将打印出数字而不是字符串组合,代码:

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);
}

the output of the arraylist is numbers and not the string chars, help! arraylist的输出是数字而不是字符串char,请帮助!

Just change these three lines 只需更改这三行

From: 从:

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

To: 至:

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

Alternate: change one line: 备选:更改一行:

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

char cannot be concatenated like string . char不能像string那样string The + is interpreted numerically. +数字解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM