Results 1 to 3 of 3
  1. #1
    clickhere9 is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Adjacency Matrix with an ArrayList of ArrayLists

    Hi guys,

    I am very new to Java and programming in general so please excuse any noob things I do/ask

    I am trying to create an Adjacency Matrix for a graph and decided to use an ArrayList of ArrayLists (is there a better way to represent a 2D array?)

    Java Code:
    private ArrayList<ArrayList<int>> adjacencyMatrix;
    I've also got another ArrayList called listOfNodes which just contains all the nodes in the graphs. I use the index of the nodes in this list as the row & column indexes in the adjacencyMatrix.

    Now, I'm writing a method to remove a node and I was just wondering if this will work:

    Java Code:
    	public void removeNode (T a){
    		//For every column
    		for (int i = 0; i<listOfNodes.size(); i++){
    			
    			int index = listOfNodes.indexOf(a);
    			
    			if (adjacencyMatrix.get(i) != null){
    				adjacencyMatrix.get(i).get(index) = 0; 
    			} else {
    				//exit the moment we encounter a null column (means end of graph)
    				return;
    			}
    		}
    		//Remove node 'a's column
    		adjacencyMatrix.remove(index);
    		//Remove node in the listOfNodes
    		listofNodes.remove(index);
    	}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Adjacency Matrix with an ArrayList of ArrayLists

    Quote Originally Posted by clickhere9 View Post
    Now, I'm writing a method to remove a node and I was just wondering if this will work:
    Why ask us? You can easily test it yourself by compiling and running your code.

  3. #3
    clickhere9 is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: Adjacency Matrix with an ArrayList of ArrayLists

    Oh hang on, I just realised why that wouldn't work. I changed this line:

    Java Code:
                adjacencyMatrix.get(i).get(index) = 0;
    to:

    Java Code:
    	adjacencyMatrix.get(i).remove(index);
    Hopefully that should do it?

Similar Threads

  1. Arraylist of arraylists
    By mike_ledis in forum New To Java
    Replies: 5
    Last Post: 04-03-2012, 06:18 PM
  2. Arraylist of arraylists
    By mike_ledis in forum Advanced Java
    Replies: 4
    Last Post: 04-03-2012, 05:45 PM
  3. Add lines of a matrix in arrayList
    By zenitis in forum New To Java
    Replies: 5
    Last Post: 05-04-2011, 09:01 AM
  4. ArrayList of ArrayLists
    By Dipke in forum New To Java
    Replies: 4
    Last Post: 12-29-2010, 08:56 AM
  5. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 10:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •