简体   繁体   中英

Trying to create a 2D Array from a custom Object being a Pair class. Won't compile

So I am trying to create a 2D array of nodes which are basically 2D plot coordinates. The first column so to speak (ie. [0][0], [1,0] ...) will be the head element wheras all points in the same row following it are successors which link to the first point. From this I am implementing some simple search algorithms. The class I have created (MyPair) simply takes two arguments being aVal, bVal which are both ints. However I don't know how to put these custom objects a 2D array without compilation errors. A snippet of the code follows.

    public class GraphNodes {
public MyPair[][] graphNodes;

public GraphNodes(){
    this.graphNodes = new MyPair[][] 
            {{(0,0), (1,0), (0,1)},
            {(0,1), (0,0), (1,1), (0,2)},
            {(0,2), (0,3), (0,1)},
            {(0,3), (0,2), (0,4)},
            {(0,4), (0,3), (0,5)},
            {(0,5), (0,6), (1,5), (0,4)},
            {(0,6), (1,6), (0,5)},
            {(1,0), (0,0), (1,1), (2,0)},
            {(1,1), (1,2), (2,1), (1,0), (0,1)},

I thought doing it this way would work but I am getting an error saying The left-hand side of an assignment must be a variable in eclipse. Any ideas?

(0,0) cannot be converted by the compiler to your MyPair class automatically.

You have to invoke a constructor such as new MyPair(0,0) (assuming you have such a constructor).

If you have such a constructor, you can replace any (x,y) in your code with new MyPair(x,y) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM