2

note: BJ Myers comment was useful and was the answer in fact. However, as it was a comment, I couldn't mark that as an answer but I've placed the corrected code (using his advice) at the end of this question.

Original question below continues:

This situation may look weird at first but here is what I intend to do:

Similar to the syntax in Python, instead of creating a multidimensional array (a 2-d array, to be exact), I want to create an array of arrays (a vector of vectors, in fact).

I'm aware that C# will not let me create pointers in safe code, but I'm still curious whether there is a safer way to accomplish this task w/o getting of the safe code limits.

So, I came up with the code below but couldn't figure out how to extract a specific row from the array (as shown between the comment lines).

Is it possible to pass the r'th row at once or do I need to create another temporary storage for r'th row and then pass that temporary vector through?

(System: Windows-10, VS-2013, C#)

using System;

public class Vector {
    public double[] data;

    public Vector(double[] data) {
        this.data = new double[data.GetLength(0)];
        this.data = data;
    }
}

public class Matrix {
    private int row, col;

    public Matrix(double[,] data) {
        this.row = data.GetLength(0);
        this.col = data.GetLength(1);
        Vector[] v = new Vector[this.row];

        for (int r = 0; r < this.row; r++) {
            // ****** this line below ******
            v[r] = new Vector(data[r,???]);
            // ****** how to extract the r'th row ******
        }
    }

    static void Main(string[] args) {
        double[,] data = { { 9.0, 8.0, 7.0 }, { 5.0, 6.0, 4.0 }, { 3.0, 2.0, 2.0 } };
        Matrix A = new Matrix(data);
        Console.ReadLine();
    }
}

The corrected code is below:

using System;

public class Vector {
    public double[] data;

    public Vector(double[] data) {
        this.data = new double[data.GetLength(0)];
        this.data = data;
        for (int i = 0; i < data.GetLength(0); i++) {
            Console.Write("{0: 0.000 }", this.data[i]);
        }
        Console.WriteLine();
    }
}

public class Matrix {
    private int row, col;

    public Matrix(double[][] data) {
        this.row = data.GetLength(0);
        this.col = data[0].GetLength(0);
        Vector[] v = new Vector[this.row];

        for (int r = 0; r < row; r++) {
            v[r] = new Vector(data[r]);
        }

        Console.WriteLine("rows: " + this.row.ToString());
        Console.WriteLine("cols: " + this.col.ToString());
    }

    static void Main(string[] args) {
        double[][] data = { new double[] { 9.0, 8.0, 7.0 }, 
                            new double[] { 5.0, 6.0, 4.0 }, 
                            new double[] { 3.0, 2.0, 2.0 } };
        Matrix A = new Matrix(data);
        Console.ReadLine();
    }
}
2
  • 4
    double[,] creates a 2-dimensional array. If you want an array of arrays (also known as a jagged array), you need to use double[][]. More info here. Commented Dec 18, 2015 at 22:34
  • Ok! I've replaced the multidimensional array with jagged one (didn't know the difference between the two; thanks a lot @BJMyers). And now, I'm able to pass row by row. However, this time I'm stuck when initializing the jagged array. It seems, curly brackets won't work here (sorry for my level of knowledge in C#). Commented Dec 18, 2015 at 22:55

1 Answer 1

1

Well, you want to make an array class and acess like one? Make an indexer. what is an indexer? - it's a way to make your class accessible like an array.

Look over the link for examples, I'll help you with your specific case.

public class Vector {

public double[] data;
public double this[int i]
{
    get
    {
        // This indexer is very simple, and just returns or sets
        // the corresponding element from the internal array.
        return data[i];
    }
    set
    {
        data[i] = value;
    }
}
public Vector(double[] data) {
    this.data = new double[data.GetLength(0)];
    this.data = data;
}
}

once it's defined like so, this is perfectly valid:

double elementArray = new double[data.GetLength(1)]; // declaring an array, the size of the second dimention of the data array.
for(int i =0; i<data.GetLength(1);i++)
{
 elementArray[i] = data[r,i]; // adding all the elements to the list
}
v[r] = new Vector(elementArray);

EDIT: BJ Myers' comment is right, this solution works perfectly for a jagged array too, but make sure that you declare it properly like he mentioned.

EDIT 2: Using a list is pointless here, changed the stracture to an array.

Sign up to request clarification or add additional context in comments.

2 Comments

fix your typo here v[r] = new Vector(ElementList.toArray()); it's .ToArray() not .toArray()
@MethodMan Thanks, I changed it to an array instead of a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.