要列出的C#字节[]<;字节[]>;
我试图将字节[]数组“a”放入列表“b”,但它不起作用。假设我有这个字节数组'a'要列出的C#字节[]<;字节[]>;,c#,C#,我试图将字节[]数组“a”放入列表“b”,但它不起作用。假设我有这个字节数组'a' 12344 23425 34426 34533 我想把它放到一个4项(#行)列表中,但这不起作用。(设置中间字节[],然后添加它) byte[]a={1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3}; 列表b=新列表(); 字节[]中间字节=新字节[5]; 对于(int u=0;u您需要在每次迭代中重新分配inter_byte,否则它将被重用,您将替换行。您的字节数组是引用类型,这意
12344
23425
34426
34533
我想把它放到一个4项(#行)列表中,但这不起作用。(设置中间字节[],然后添加它)
byte[]a={1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3};
列表b=新列表();
字节[]中间字节=新字节[5];
对于(int u=0;u您需要在每次迭代中重新分配inter_byte
,否则它将被重用,您将替换行。您的字节数组是引用类型,这意味着在每个循环中更改它会更改存储的数据。在每个循环中声明它应该起作用:
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[]> b = new List<byte[]>();
for (int u=0; u<4; u++)
{
byte[] inter_byte= new byte[5];
for (int p=0; p<5; p++)
{
inter_byte[p] = a[(5*u) + p];
}
b.Add(inter_byte);
}
byte[]a={1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3};
列表b=新列表();
for(int u=0;uinter\u byte
是对字节数组的引用。您只分配一次实际字节数组(使用新字节[5]
。您需要在循环中执行此操作。t尝试以下操作:
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[] b = new List<byte[]>();
for (int u=0; u<4; u++)
{
byte[] inter_byte= new byte[5];
for (int p=0; p<5; p++)
{
inter_byte[u] = file[(5*u) + p];
}
b.Add(inter_byte);
}
byte[]a={1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3};
列表var a=新字节[]
{
1, 2, 3, 4, 4,
2, 3, 4, 2, 5,
3, 4, 4, 2, 6,
3, 4, 5, 3, 3
};
var b=新列表();
int-groupSize=5;
for(int i=0;i
像这样的东西应该可以…(除非我误解了这个问题)
listb=a.Select((by,i)=>new{group=i/5,value=by})
.GroupBy(item=>item.group)
.Select(group=>group.Select(v=>v.value).ToArray()
.ToList();
将字节分组为5个数组并放入一个列表中。这里有一个很好的扩展方法,可以实现您想要做的事情,但是它更安全一些,因为它不会遇到超出范围的问题
public static IList<T[]> GroupArray<T>(this T[] array, int groupSize)
{
if (array == null)
throw new ArgumentNullException("array");
if (groupSize <= 0)
throw new ArgumentException("Group size must be greater than 0.", "groupSize");
IList<T[]> list = new List<T[]>();
T[] temp = new T[groupSize];
for (int i = 0; i < array.Length; i++)
{
if ((i % groupSize) == 0)
{
temp = new T[groupSize];
list.Add(temp);
}
temp[(i % groupSize)] = array[i];
}
return list;
}
公共静态IList GroupArray(此T[]数组,int groupSize)
{
if(数组==null)
抛出新的ArgumentNullException(“数组”);
if(groupSizebyte[]a={1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3};
列表b=新列表();
对于(intu=0;用户界面更喜欢你的,而不是我的):
var a = new byte[]
{
1, 2, 3, 4, 4,
2, 3, 4, 2, 5,
3, 4, 4, 2, 6,
3, 4, 5, 3, 3
};
var b = new List<byte[]>();
int groupSize = 5;
for (int i = 0; i < a.Length; i += groupSize)
{
int interSize = Math.Min(a.Length - i, groupSize);
var interByte = new byte[interSize];
Buffer.BlockCopy(a, i, interByte, 0, interSize);
b.Add(interByte);
}
List<byte[]> b = a.Select((by, i) => new { group = i / 5, value = by })
.GroupBy(item => item.group)
.Select(group => group.Select(v => v.value).ToArray())
.ToList();
public static IList<T[]> GroupArray<T>(this T[] array, int groupSize)
{
if (array == null)
throw new ArgumentNullException("array");
if (groupSize <= 0)
throw new ArgumentException("Group size must be greater than 0.", "groupSize");
IList<T[]> list = new List<T[]>();
T[] temp = new T[groupSize];
for (int i = 0; i < array.Length; i++)
{
if ((i % groupSize) == 0)
{
temp = new T[groupSize];
list.Add(temp);
}
temp[(i % groupSize)] = array[i];
}
return list;
}
Byte[] myByte = { 1, 2, 3, 4, 4, 2, 3, 4, 2, 5, 3, 4, 4, 2, 6, 3, 4, 5, 3, 3 };
IList<Byte[]> myList = myByte.GroupArray(5);
foreach (var item in myList)
{
Console.Write(item + " ");
foreach (var item2 in item)
{
Console.Write(item2);
}
Console.WriteLine();
}
byte[] a = {1,2,3,4,4,2,3,4,2,5,3,4,4,2,6,3,4,5,3,3};
List<byte[]> b = new List<byte[]>();
for (int u=0; u<a.Count; u+=5)
{
b.Add(a.Skip(u).Take(5).ToArray());
}