The problem with your attribute grafoo
, it is a matrix of int
primitive, when to receive another matrix, need to be more complex.
There are two approaches:
The first is to use the Object type in grafoo
, that facilitates much in the access to this second matrix, since it is enough to access an edge of the grafoo
, which returns an array, and then access the array again. The Code would look like this:
public class Graph {
/** Atributos da classe Graph */
Object [][] grafoo; // o grafo está representada por uma matriz de adjacência
/** Construtor da classe Graph */
public Graph (int dim)
{
grafoo = new Object [dim][dim];
// Nesse caso não precisamos inicializar o grafoo, porque ele ja esta com todos os valores null, se fosse int também não precisaria,
// já que por ser diferentemente do C, a JVM atribui valor zero para as áreas de memória após a alocação.
}
/** Definição do Método */
public void add_edge(int i, int j) //recebe dois nós e adiciona ao grafo uma aresta de um nó para outro com uma matriz de NxM.
{
int[][] E = new int[N][M];
grafoo[i][j] = E; // Agora isso é totalmente válido, no caso anterior gerava erro de compilação.
}
public int[][] get_edge(int i, int j)
{
return (int[][]) grafoo[i][j]; // Retorna a matriz relativa à aresta i,j
}
public void update_edge(int i, int j, int[][] new_edge) // Atualização por sobreescrita
{
grafoo[i][j] = new_edge;
}
public void update_edge(int i, int j, int k, int l, int value) // Atualização de célula
{
int[][] matrix = (int[][]) grafoo[i][j];
matrix[k][l] = value;
}
}
This approach is much simpler, but has the annoyance that, with complexity increasing, if you change what you keep inside the edges of the grafoo
, you will have to review all the sites that access the grafoo
so that no error occurs. Specifically in cast
which is done in the get_edge
.
The second alternative is to use int[][]
instead of Object
, leaving the statement of grafoo
thus: int [][][][]
, which I find very strange and more confusing to access, however any "mistake" will result in compilation error and not in runtime
as in the first alternative.
This question is very broad, need to define which problem is occurring.
– Wakim
The problem is I don’t know how to add a matrix to an edge. The graph is constructed so as to be represented by an adjacency matrix (see constructor method), and then my goal is to create a function (add_edge) that takes 2 us and adds an edge matrix.
– user8885
Know if q always q the graph is represented by an adjacency matrix and qnd there is an edge between 2 nos, the respective input and 1. In my case, I wanted q instead of representing an edge by 1, to put in that input an Nxm matrix (example 4x5).
– user8885