0

I know you can use the list.ToArray() method to convert list to an array, but I want to be able to access the individual characters of the string after conversion. For example, if the first element of the string list is "sgoth", once I convert it to an array, I want to be able to access an individual element of the array like myArray[0,0] which would give me the first element of the first string. How would I accomplish this when converting from a list to an array?

6
  • 4
    You can but you should do it like myArray[0][0], otherwise you have a multidimensional array which is very different than an array of arrays. Commented Apr 20, 2022 at 15:11
  • What's the problem with myArrray[0][0]? Commented Apr 20, 2022 at 15:12
  • 1
    Lists, arrays and indeed strings support (almost) exactly the same indexing, so it's not sure what your issue would be of one versus the other. Commented Apr 20, 2022 at 15:13
  • 1
    @igor You have that the wrong way around - a multidimensional array cannot be jagged, but an array of arrays can be. But for an array of strings of different lengths, it HAS to be jagged, obviously - since the strings can be different lengths. Commented Apr 20, 2022 at 15:13
  • 2
    @Russell You can't do this with a multidimensional array of chars UNLESS all the strings are the same length (or you're happy with padding out some of the rows with a special character such as '\0') Commented Apr 20, 2022 at 15:16

4 Answers 4

1

I think using jagged array would be better, but if you absolutely need to have array accessed with [,] as you decriped, you could do something following:

using System;
using System.Linq;
List<string> ListOfStrings = new List<string>() { "Test", "QWERTY" };
string[,] ArrOfStrings = new string[ListOfStrings.Count, ListOfStrings.Max(s => s.Length)];
for(int i = 0; i < ListOfStrings.Count; i++){
    for(int j = 0; j < ListOfStrings[i].Length; j++){
        ArrOfStrings[i,j] = ListOfStrings[i][j].ToString();   
    }
}
Console.WriteLine(ArrOfStrings[1,2]);
Sign up to request clarification or add additional context in comments.

1 Comment

A char[,] would be better: each cell does store a character after all
1

You can do that by using the string indexer on each particular string in array, for example

var strList = new List<String>() { "foo", "boo" };

var strarr = strList.ToArray();

foreach (var str in strarr)
{
    Console.Write(str[0]);
}

Will print you "fb"

Or by using double indexer, for example you can access the first char of the first string like this:

Console.WriteLine(strarr[0][0]);

Will just print you "f"

Comments

1

How would I accomplish this when converting from a list to an array?

It's perhaps most efficient to convert nothing

You have a List<string>, it can be accessed in a jagged fashion myArray[0][0] to get the first char of the first string, but you say you want to access it in multidim fashion, like myArray[0,0]..

..so you can make an adapter:

    public class MultiDimFlavoredJagged
    {
        List<string> _x;

        public MultiDimFlavoredJagged(List<string> x)
        {
            _x = x;
        }

        public char this[int x, int y] => _x[x][y];
    }

Now you can:

var j = new MultiDimFlavoredJagged(myArray);

var firstCharOfFirstString = j[0,0];

Comments

0

//See example below;-

//Create a new list

List MyList = new List();

        //add elements to the list
        MyList.Add("Ade");
        MyList.Add("Raphael");
        MyList.Add("Bayo");
        MyList.Add("Paul");

        //creation of an array from the list
        string[] MyArray = MyList.ToArray();

1 Comment

What did it gain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.