简体   繁体   English

如何在 c# 中用字符填充锯齿状数组

[英]How can I populate jagged array with chars in c#

I am struggling to populate jagged array with chars My goal is to read string.我正在努力用字符填充锯齿状数组我的目标是读取字符串。 slice them into chars, then populate jagged array with those chars将它们切成字符,然后用这些字符填充锯齿状数组

            "Lorem",
            5,
            new char[][]
            {
                new char[] { 'L', 'o', 'r', 'e', 'm' },
            },

or或者

            "Loremipsumdolorsitamet",
            5,
            new char[][]
            {
                new char[] { 'L', 'o', 'r', 'e', 'm' },
                new char[] { 'i', 'p', 's', 'u', 'm' },
                new char[] { 'd', 'o', 'l', 'o', 'r' },
                new char[] { 's', 'i', 't', 'a', 'm' },
                new char[] { 'e', 't' },
            },

like the example.就像这个例子。 above ints under the string represent arraySize(ie number of columns) and streamReader(see code under) is the string itself.上面的字符串下的整数代表arraySize(即列数)和streamReader(见下面的代码)是字符串本身。 I am using System.IO and System.Text我正在使用 System.IO 和 System.Text

        var read = streamReader.ReadToEnd().ToCharArray();
        if (read.Length == 0)
        {
            return Array.Empty<char[]>();
        }

        char[][] jagArray = new char[read.Length / 5][];

        for (int p = 0; p < read.Length; p++)
        {
            for (int i = 0; i < read.Length / arraySize; i++)
            {
                jagArray[i] = new char[arraySize];

                for (int j = 0; j < arraySize; j++)
                {
                    jagArray[i][j] = read[p];
                }
            }
        }

        return jagArray;

I tried this code, but obviously it doesnt work.我试过这段代码,但显然它不起作用。

string str = "Loremipsumdolorsitamet";
int slice = 5;
List<List<char>> list = new List<List<char>>();
List<char> list2 = new List<char>();
int limit = str.Length/5 +1;
for (int i = 0; i < limit; i++)
{
    if (str.Length <= 5)
    {
        slice = str.Length;
    }
    for (int j = 0; j < slice; j++)
    {
        list2.Add(str[j]);
    }
    str = str.Remove(0, slice);
    list.Add(list2);
    list2 = new List<char>();
}

Hopefully I understood you correctly, This is my very scuffed way of doing it, hopefully some of the more experienced users can provide some info to improve this.希望我对你的理解是正确的,这是我做这件事的非常糟糕的方式,希望一些更有经验的用户可以提供一些信息来改进这一点。

EDIT.编辑。 OUTPUT: OUTPUT:

foreach(var a in list)
{
    foreach (var b in a)
    {
        Console.Write(b + " ");
    }
    Console.WriteLine();
}

L o r e m
i p s u m
d o l o r
s i t a m
e t

Use this method to split a single char[] array into array of arrays, each of which is no longer than cols characters length:使用此方法将单个char[]数组拆分为 arrays 的数组,每个数组的长度不超过cols个字符长度:

public static char[][] ToJaggedArray(char[] chars, int cols)
{
    int rows = (chars.Length + cols - 1) / cols;
    char[][] jagArray = new char[rows][];
    for (int i = 0; i < rows; i++)
    {
        jagArray[i] = new char[Math.Min(cols, chars.Length - cols * i)];
    }

    for (int i = 0; i < chars.Length; i++)
    {
        int row = i / cols;
        int col = i % cols;
        jagArray[row][col] = chars[i];
    }

    return jagArray;
}

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

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