2D数组无法解析为变量

时间:2014-11-17 19:49:13

标签: java arrays graph int

我正在做另一项家庭作业,我遇到了这个问题。我的2D int [] [] graph1(数组)“无法解析为变量”?

基本上,我将一些图表转换为2D数组,必须将其上传到程序中,如果它们是PATH或CIRCUIT则会读取。一个是有向图,另一个(我现在在其中)是无向的。

这是我到目前为止所做的:

boolean undirectedCircuit (int [][] graph)
{
    //graph = graph1(graph1(null));

    int edgeCounter = 0;

    for (int edge = 0; edge < graph.length; edge++)
    {
        /* SET FOR WHEN 1s are found in array: edgeCounter++;*/
        if(graph[4][4] == '1')
        {
            edgeCounter++;
            System.out.println("edgeCounter found '1' " + edgeCounter + "times");
        }
    }

    if (edgeCounter % 2 == 0)
    {
        System.out.println("This is a circuit!");
        //return true;
    }
    else System.out.println("This is not a circuit!!");
    return false;
    }

public void go ()
{
    graph1 = new int[][] //This line is complained about.
            {
            {0,1,1,1,0},
            {1,0,0,0,1},
            {1,0,0,1,0},
            {1,0,1,0,1},
            {0,1,0,1,0}
            };

    undirectedCircuit(graph1); //This is complained about.
} 

任何建议都会很棒!

2 个答案:

答案 0 :(得分:1)

  

graph1(array)“无法解析为变量

您尚未声明graph1,因此编译器无法解析符号graph1

解决方案: -

graph1 = new int[][] //This line is complained about.
            {
            {0,1,1,1,0},
            {1,0,0,0,1},
            {1,0,0,1,0},
            {1,0,1,0,1},
            {0,1,0,1,0}
            };

应该是

int[][] graph1 = 
            {
            {0,1,1,1,0},
            {1,0,0,0,1},
            {1,0,0,1,0},
            {1,0,1,0,1},
            {0,1,0,1,0}
            };

答案 1 :(得分:0)

您从未声明graph1

您应该将graph1 = new int[][] ...更改为int[][] graph1 = new int[][] ...

相关问题