如何将 2 个数组连接成 1 个数组
How can I concatenate 2 arrays into 1 array
我有两个列表框,它们的字符如下:
Listbox1 Listbox2
Model1 Price1
Model2 Price2
Model3 Price3
Model4 Price4
我想在 listbox3 中显示两个列表,这样:
Listbox
Model1,Price1
Model2,Price2
Model3,Price3
Model4,Price4
我尝试合并列表,但出现错误:
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
///
///Modelos
///
public string[] _EModelsArray;
public string[] _ModelsArray;
public string[] _UnionArray;
StreamReader _reader = new StreamReader(@".\MODELOS.txt"); // Abre el archivo de texto
List<string> _info = new List<string>();
while(!_reader.EndOfStream)
{
string _line = _reader.ReadLine().Trim();
string [] _tokens = _line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
///
/// crear el listado
///
var _Model = new List<string>();
var _EModel = new List<string>();
var _Union = new List<string>();
//var _EQty = new List<string>();
if (_tokens.Length == 2)
{
_Model.Add(_tokens[0]);
_EModel.Add(_tokens[1]);
//_EQty.Add(_tokens[2]);
}
//else
//MessageBox.Show("Error!!!");
///
/// Convertir en array
///
_ModelsArray = _Model.ToArray();
_EModelsArray = _EModel.ToArray();
foreach (var item in _ModelsArray)
{
listBox1.Items.Add(item);
}
foreach (var item in _EModelsArray)
{
listbx2.Items.Add(item);
}
for (int i = 0; i <=30; i ++)
{
_Union[i] = _Model[i] + _EModel[i];
}
_UnionArray = _Union.ToArray();
foreach (var item in _UnionArray)
{
listbx_union.Items.Add(item);
}
而我正在为:
Listbox3
Model1,Price1
Model2,Price2
Model3,Price3
Model4,Price4
型号和价格合并为 1 行。
快速查看原始代码后有两点需要更改。首先不要使用文字数字来创建联合列表。其次,联合列表还没有元素,所以你应该使用 .Add()。像这样:
for (int i = 0; i < _Model.Length; i ++)
{
_Union.Add(_Model[i] + "," + _EModel[i]);
}
您可以利用 LINQ .Select()
重载之一。
var listBox1 = new[] { "Model1", "Model2", "Model3", "Model4" };
var listBox2 = new[] { "Price1", "Price2", "Price3", "Price4" };
var listbox3 = listBox1.Select((value, element) => $"{value},{listBox2[element]}");
此重载使用 Func<string, int, string>
lambda 不仅使用查询中的枚举项目,还使用项目编号。这允许您使用项目编号作为第二个数组的数组位置。
您可能会从 for
循环声明 for (int i = 0; i <=30; i ++)
中得到错误。您为该循环提供了预定的恰好 31 次迭代。为减轻此错误,您必须在每个数组中包含 >=31 个元素。
您收到该错误的原因是您的 hard-coded 值为 30
,并且列表中的项目少于 31
,因此您试图访问循环条件中不存在的索引:
for (int i = 0; i <= 30; i ++) // This will fail if there are less than 31 items
解决此问题的一种方法是在循环条件中简单地使用列表的 Count
属性:
for (int i = 0; i <= _Model.Count; i++)
但是,创建一个 class 包含您要为文件中的每一行设置的属性,然后创建该 class 的列表可能更有意义。然后,您可以将 ListBox
项目设置为 class 的特定 属性。
例如,我们可以创建 class Model
看起来像:
class Model
{
public string Name { get; set; }
public string Price { get; set; }
public string Both => $"{Name},{Price}";
}
然后我们可以创建一个方法来读取一个文件和returns一个基于文件内容的class列表:
public static List<Model> GetModels(string filePath)
{
return File?
.ReadAllLines(filePath)
.Where(line => line.Contains(':'))
.Select(line => line.Split(':'))
.Select(lineParts => new Model { Name = lineParts[0], Price = lineParts[1] })
.ToList();
}
现在我们只需调用此方法即可获取单个列表,然后使用列表中每个项目的信息更新我们的列表框:
foreach (var model in GetModels(@".\MODELOS.txt"))
{
listBox1.Items.Add(model.Name);
listbx2.Items.Add(model.Price);
listbx_union.Items.Add(model.Both);
}
我有两个列表框,它们的字符如下:
Listbox1 Listbox2
Model1 Price1
Model2 Price2
Model3 Price3
Model4 Price4
我想在 listbox3 中显示两个列表,这样:
Listbox
Model1,Price1
Model2,Price2
Model3,Price3
Model4,Price4
我尝试合并列表,但出现错误:
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
///
///Modelos
///
public string[] _EModelsArray;
public string[] _ModelsArray;
public string[] _UnionArray;
StreamReader _reader = new StreamReader(@".\MODELOS.txt"); // Abre el archivo de texto
List<string> _info = new List<string>();
while(!_reader.EndOfStream)
{
string _line = _reader.ReadLine().Trim();
string [] _tokens = _line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
///
/// crear el listado
///
var _Model = new List<string>();
var _EModel = new List<string>();
var _Union = new List<string>();
//var _EQty = new List<string>();
if (_tokens.Length == 2)
{
_Model.Add(_tokens[0]);
_EModel.Add(_tokens[1]);
//_EQty.Add(_tokens[2]);
}
//else
//MessageBox.Show("Error!!!");
///
/// Convertir en array
///
_ModelsArray = _Model.ToArray();
_EModelsArray = _EModel.ToArray();
foreach (var item in _ModelsArray)
{
listBox1.Items.Add(item);
}
foreach (var item in _EModelsArray)
{
listbx2.Items.Add(item);
}
for (int i = 0; i <=30; i ++)
{
_Union[i] = _Model[i] + _EModel[i];
}
_UnionArray = _Union.ToArray();
foreach (var item in _UnionArray)
{
listbx_union.Items.Add(item);
}
而我正在为:
Listbox3
Model1,Price1
Model2,Price2
Model3,Price3
Model4,Price4
型号和价格合并为 1 行。
快速查看原始代码后有两点需要更改。首先不要使用文字数字来创建联合列表。其次,联合列表还没有元素,所以你应该使用 .Add()。像这样:
for (int i = 0; i < _Model.Length; i ++)
{
_Union.Add(_Model[i] + "," + _EModel[i]);
}
您可以利用 LINQ .Select()
重载之一。
var listBox1 = new[] { "Model1", "Model2", "Model3", "Model4" };
var listBox2 = new[] { "Price1", "Price2", "Price3", "Price4" };
var listbox3 = listBox1.Select((value, element) => $"{value},{listBox2[element]}");
此重载使用 Func<string, int, string>
lambda 不仅使用查询中的枚举项目,还使用项目编号。这允许您使用项目编号作为第二个数组的数组位置。
您可能会从 for
循环声明 for (int i = 0; i <=30; i ++)
中得到错误。您为该循环提供了预定的恰好 31 次迭代。为减轻此错误,您必须在每个数组中包含 >=31 个元素。
您收到该错误的原因是您的 hard-coded 值为 30
,并且列表中的项目少于 31
,因此您试图访问循环条件中不存在的索引:
for (int i = 0; i <= 30; i ++) // This will fail if there are less than 31 items
解决此问题的一种方法是在循环条件中简单地使用列表的 Count
属性:
for (int i = 0; i <= _Model.Count; i++)
但是,创建一个 class 包含您要为文件中的每一行设置的属性,然后创建该 class 的列表可能更有意义。然后,您可以将 ListBox
项目设置为 class 的特定 属性。
例如,我们可以创建 class Model
看起来像:
class Model
{
public string Name { get; set; }
public string Price { get; set; }
public string Both => $"{Name},{Price}";
}
然后我们可以创建一个方法来读取一个文件和returns一个基于文件内容的class列表:
public static List<Model> GetModels(string filePath)
{
return File?
.ReadAllLines(filePath)
.Where(line => line.Contains(':'))
.Select(line => line.Split(':'))
.Select(lineParts => new Model { Name = lineParts[0], Price = lineParts[1] })
.ToList();
}
现在我们只需调用此方法即可获取单个列表,然后使用列表中每个项目的信息更新我们的列表框:
foreach (var model in GetModels(@".\MODELOS.txt"))
{
listBox1.Items.Add(model.Name);
listbx2.Items.Add(model.Price);
listbx_union.Items.Add(model.Both);
}