简体   繁体   中英

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:

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!

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 . The + is interpreted numerically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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