簡體   English   中英

2D數組無法解析為變量

[英]2D array cannot be resolved to a variable

我正在從事另一項家庭作業,而我一直陷入這個問題。 我的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.
} 

任何建議都很好!

graph1(數組)“無法解析為變量

您尚未聲明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}
            };

您從未聲明graph1

您應該將graph1 = new int[][] ...更改為int[][] graph1 = new int[][] ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM