如何解决如何制作一个整数数组的列表?
| 我想拥有2个Int32值的数组,例如: Int32 x
Int32 y
我想列出这些数组。
如何声明和初始化此数组和列表?
填充列表后,如何访问列表的成员?
解决方法
听起来您正在尝试将数组转换为数据结构,而不是通过顺序存储值来实现。不要这样做。了解如何使用更高级的数据结构以发挥自己的优势。
您提到了带有
x
和y
值的Point
类型。上课怎么样?
class Point
{
public readonly int X;
public readonly int Y;
public Point( int x,int y )
{
X = x;
Y = y;
}
}
现在,您可以创建新类型的实例并将其添加到列表中,从而简化整个过程,并确保您不会滑倒,并在应有y
的数组中添加x
。
List<Point> ls = new List<Point>();
ls.Add( new Point( 0,0 ) );
ls.Add( new Point( 10,10 ) );
ls.Add( new Point( 100,100 ) );
无论如何,最好阅读一下如何使用C#创建自己的数据结构。学习如何以一种易于使用的方式适当地存储数据有很多好处。
, List<int[]> l = new List<int[]>();
l.Add(new int[] { 1,2 });
l.Add(new int[] { 3,4 });
int a = l[1][0]; // a == 3
, 您所需的信息不足。但是这里是初始化Int32数组的通用列表的基本示例。我希望这有帮助
Int32 x = 1;
Int32 y = 2;
// example of declaring a list of int32 arrays
var list = new List<Int32[]> {
new Int32[] {x,y}
};
// accessing x
list[0][0] = 1;
// accessing y
list[0][1] = 1;
, 好吧,有两种类型的数组。多维数组和锯齿状数组。您可以使用任何一种(有关它们之间的差异的更多信息,请访问http://msdn.microsoft.com/zh-cn/library/aa288453(v=vs.71).aspx)。
锯齿状数组的示例:
Int32[][] = new Int32[] { new Int32[] {1,2},new Int32[] {3,4}};
多维数组的示例:
Int32[,] = new Int32[,] {{1,{3,4}};
希望能帮助您将事情弄清楚。如果您的意思是实际列表,请查看其他答案。
, 使用仅包含两个int32的元组列表:
List<Tuple<int,int>> myList = new List<Tuple<int,int>>();
var item = new Tuple<int,int>(25,3);
myList[0] = new Tuple<int,int>(20,9);//acess to list items by index index
myList.Add(item);//insert item to collection
myList.IndexOf(item);//get the index of item
myList.Remove(item);//remove item from collection
在第二个列表(例如List<List<int,int>>
或List<int[]>
)上使用List<Tuple<int,int>>
的好处是,您可以将列表项显式强制为两个整数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。