开发者

I'm having trouble with the Node data structure -- trying to make it more efficient with loops

开发者 https://www.devze.com 2023-04-10 01:34 出处:网络
So I\'m having a bit of an issue with my code. My objective is to take an adjacency matrix from a file and input it into a 2d array. I was able to do that. Now I\'m using the data structure for Breadt

So I'm having a bit of an issue with my code. My objective is to take an adjacency matrix from a file and input it into a 2d array. I was able to do that. Now I'm using the data structure for Breadth First Search and Nodes to work with that array.

Right now, I'm trying to simply create new Nodes, but I cannot make nodes due to being a char. Here let me post my full code. I'll post the error below.


package javaapplication1;
import java.io.*;
import java.util.*;
import tio.*;
import java.lang.*;



public class JavaApplication1 {
    private static int row = 0;
    private static int col = 0;
    private static int n = 20;
    private static int[][]adjMatrix = new int[n][n];


public static int[][] adjMatrix() throws FileNotFoundException, IOException{
   //int n = 20;
   //int row = 0;
   //int col = 0;
   //int[][]adjMatrix = new int[n][n];
   String file =   ("C:\\Users\\David\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\adjmatrix.txt");

   BufferedReader in = new BufferedReader(new FileReader(file));
   String line;
   //System.out.println(in.readLine());


   int k = 0;
     while ((line = in.readLine()) != null){
         //System.out.println(row);
         String[] temp = line.split("\\s+");
        for(col = 0; col < adjMatrix[row].length; col++){
           adjMatrix[row][col] = Integer.parseInt(temp[col]);
          // System.out.println(" " + temp[col] + " " + col);
        }
row++;
     }  //end while
   //System.out.print(array[4][1]);
   in.close();
   return adjMatrix;



} // endclass
 public static void main(String[] args)
    throws IOException{
    adjMatrix();
    // Create the nodes (20 based off adj matrix given
    Node nA =new Node('1');
    Node nB =new Node('2');
    Node nC = new Node('3');
    Node nD = new Node('4'); 
    Node nE = new Node('5');
    Node nF=new Node('6');
    Node nG=new Node('7');
    Node nH=new Node('8');
    Node nI=new Node('9');
    Node nJ=new Node('10');
    Node nK=new Node('11');
    Node nL=new Node('12');
    Node nM=new Node('13');
    Node nN=new Node('14');
    Node nO=new Node('15');
    Node nP=new Node('16');
    Node nQ=new Node('17');
    Node nR=new Node('18');
    Node nS=new Node('19');
    Node nT=new Node('20');


    // Create a graph, adding the nodes, and creating edges

    Graph g = new Graph();
    for (int i=1;i<=20;i++){
        String aString = Integer.toString(i);
        aString = n+aString;
        g.addNode(aString);
    }
//        g.addNode(nA);
//        g.addNode(nB);
//        g.addNode(nC);
//        g.addNode(nD);
//        g.addNode(nE);
//        g.addNode(nF);
//        g.addNode(nG);
//        g.addNode(nH);
//        g.addNode(nI);
//        g.addNode(nJ);
//        g.addNode(nK);
//        g.addNode(nL);
//        g.addNode(nM);
//        g.addNode(nN);
//        g.addNode(nO);
//        g.a开发者_开发知识库ddNode(nP);
//        g.addNode(nQ);
//        g.addNode(nR);
//        g.addNode(nS);
//        g.addNode(nT);
//        g.addNode(nU);
//        g.setRootNode(nA); 

//        g.connectNode(nA,nB);
//        g.connectNode(nA,nD);
//        g.connectNode(nA,nE);
//        g.connectNode(nA,nF);
//        g.connectNode(nA,nG);
//        
//        g.connectNode(nB,nE);
//        g.connectNode(nB,nF);
//        g.connectNode(nB,nG);
//        
//        g.connectNode(nC, nD);
//        g.connectNode(nC,nE);
//        g.connectNode(nC,nF);
//        g.connectNode(nC,nG);
//        
//        g.connectNode(nD,nE);
//        g.connectNode(nD,nF);
//        
//        g.connectNode(nE, nF);
//        g.connectNode(nE,nG);
//        
//        g.connectNode(nF,nG);
//        
//        g.connectNode(nH, nI);
//        g.connectNode(nH,nJ);
//        g.connectNode(nH,nK);

//        g.connectNode(nI,nJ);
//        g.connectNode(nI,nK);
//        g.connectNode(nI,nL);




     g.bfs();


  } // end main
} // end class

I know that is a lot of code, but that is the reason why I would really like to not brute force the g.connectNode and the g.addNode. Anyways I can implement a loop for this? Also, when I start doing "Node nJ=new Node('10');" it gives me errors since my Node is a char, any suggestions on bypassing that without breaking everything? The error is an unclosed character literal.

Here is the code for node.

public class Node {
    public char label;
    public boolean visited=false;
    public Node(char l)
    {
            this.label=l;
    }
}

Thank you for your help, and let me know if I need to edit or add anything.


First suggestion: put your nodes in an array or list, don't repeat the same thing 20 times.

Change your label's data type to String, and everything will be fine.

I am not sure how you define your Graph class. But you should really define your addNode method as

bool addNode(Node n) or something like that.

Another thing is to keep your data field private and provide getters and setters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号