user776676
user776676

Reputation: 4385

How to make a list of an array of ints?

I want to have an array of 2 Int32 values, say:

 Int32 x
 Int32 y

I want to make a list of these arrays.

Upvotes: 1

Views: 8981

Answers (5)

Jalal Said
Jalal Said

Reputation: 16162

use a list of tuple that contains two int32 only:

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

The benefit from using List<Tuple<int, int>> over a second list like List<List<int, int>> or List<int[]> is that you explicit force list items to be just two integers.

Upvotes: 1

Jess
Jess

Reputation: 8700

Well, there are two types of arrays. Multidimensional arrays, and jagged arrays. You can use either (more on their differences at http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx).

An example of a jagged array:

Int32[][] = new Int32[] { new Int32[] {1,2}, new Int32[] {3,4}};

An example of a multidimensional array:

Int32[,] = new Int32[,] {{1,2},{3,4}};

Hoped that help clear things up a bit. If you meant actual lists, then look at the other answers.

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124632

It sounds like you are trying to turn an array into a data structure that it is not by storing values in sequence. Don't do this. Learn how to use more advanced data structures to your advantage.

You mention a Point type with an x and y value. How about a class instead?

class Point
{
    public readonly int X;
    public readonly int Y;
    public Point( int x, int y )
    {
        X = x;
        Y = y;
    }
}

Now you can create instances of your new type and add them to a list, simplifying the entire process and also making sure you don't slip up down the road and add an x to your array where a y should be.

List<Point> ls = new List<Point>();
ls.Add( new Point( 0, 0 ) );
ls.Add( new Point( 10, 10 ) );
ls.Add( new Point( 100, 100 ) );

Anyway, it would be a good idea to read up on how you can create your own data structures in C#. There are many benefits to learning how to appropriately store your data in a way that is easy to work with.

Upvotes: 6

Frank
Frank

Reputation: 1132

There is not quite enough information on what you would like. But here a basic example of initializing a generic list of an Int32 array. I hope this helps

        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;

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93010

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

Upvotes: 8

Related Questions