我很難拋出IllegalStateForMatrixException。如果有人能幫助我,將不勝感激。這是我的教授告訴我要做的事情,但我仍然遇到問題。這是他給我的一個提示...投擲異常問題
調用帶有0邊的getNumberOfEdges時應引發IllegalStateForMatrixException。 調用帶有0邊的getNumberOfEdges時,應引發IllegalStateForMatrix。 所有你需要做的是檢查矩陣沒有正值。如果它不拋出IllegalStateForMatrix異常。
您還可以將布爾值設置爲false,並在您調用createMatrix時將其設置爲true。這將是一種檢查createMatrix是在addEdge之前還是之後調用的方法。然後檢查addEdge,你可以拋出異常。
class AdjacencyGraph extends Graph {
private ArrayList nodes = new ArrayList();
private int noEdges = 0;
/**
* Adjacency matrix.
*/
public int[][] adjM;
/**
* Boolean set to false, should be set true in create matrix.
*/
private boolean b = false;
/**
* Adjacency Matrix with integers.
*/
@Override
void createAdjacencyMatrix() {
adjM = new int[nodes.size()][nodes.size()];
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
for (j = 0; j < nodes.size(); j++) {
adjM[i][j] = -1;
}
b = true;
}
}
/**
* Adds the node.
*
* @param nodeName
*/
@Override
@SuppressWarnings("unchecked")
void addNode(String nodeName) {
nodes.add(nodeName);
}
/**
* Adds the edge.
*
* @param fromNode
* @param toNode
* @param weight
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
void addEdge(String fromNode, String toNode, int weight)
throws ElementNotFoundException, IllegalStateForMatrixException {
{
try {
if (b == false)
throw new IllegalStateForMatrixException("Exception");
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromNode)) {
break;
}
}
if (i == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toNode)) {
break;
}
}
if (j == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
adjM[i][j] = weight;
adjM[j][i] = weight;
noEdges++;
}
catch (ElementNotFoundException e) {
System.out.println("Exception found");
}
catch (IllegalStateForMatrixException e) {
System.out.println("Exception found");
}
}
}
/**
* Returns the number of nodes.
*
* @return number of nodes
*/
@Override
int getNumberOfNodes() {
return nodes.size();
}
/**
* Returns the number of edges
*
* @return number of edges
* @throws IllegalStateForMatrixException
*/
@Override
int getNumberOfEdges() throws IllegalStateForMatrixException {
if (nodes.size() <= 0)
throw new IllegalStateForMatrixException("Error");
return noEdges;
}
/**
* Returns the highest degree node.
*
* @return highest degree node.
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
public String getHighestDegreeNode()
throws ElementNotFoundException, IllegalStateForMatrixException {
int i;
int ansIndex = 0;
int j;
int ansCount = 0;
for (i = 0; i < nodes.size(); i++) {
int k = 0;
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1) {
k++;
}
}
if (k > ansCount) {
ansCount = k;
ansIndex = i;
}
}
return (String) nodes.get(ansIndex);
}
/**
* Cost of the edge between nodes.
*
* @param fromNode
* @param toNode
* @return returns -1
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
int costOfEdgeBetween(String fromNode, String toNode)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromNode)) {
break;
}
}
if (i == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toNode)) {
break;
}
}
if (j == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
return adjM[i][j];
}
catch (ElementNotFoundException e) {
System.out.println("Exception found");
}
return -1;
}
/**
*
* @param fromName
* @param toName
* @return false
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
boolean hasPathBetween(String fromName, String toName)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
ArrayStack<Integer> st = new ArrayStack<Integer>();
int[] visited = new int[nodes.size()];
int i;
int j;
int start;
int end;
for (i = 0; i < nodes.size(); i++) {
visited[i] = 0;
}
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromName)) {
break;
}
}
start = i;
for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toName)) {
break;
}
}
end = j;
st.push(start);
visited[start] = 1;
while (st.isEmpty() != true) {
i = st.pop();
if (i == end) {
return true;
}
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1 && visited[j] == 0) {
visited[j] = 1;
st.push(j);
}
}
}
return false;
}
catch (Exception e) {
System.out.println("Exception found");
}
return false;
}
/**
* The number of Isolated points
*
* @return ans
* @throws IllegalStateForMatrixException
*/
@Override
int numIsolatedPoints() throws IllegalStateForMatrixException {
int i;
int j;
int ans = 0;
for (i = 0; i < nodes.size(); i++) {
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1) {
break;
}
}
if (j == nodes.size()) {
ans++;
}
}
return ans;
}
/**
* Inclusiveness is the percentage of points in the graph that are not
* isolated
*
* @return ((float)i)/nodes.size()
* @throws IllegalStateForMatrixException
*/
@Override
float inclusiveness() throws IllegalStateForMatrixException {
int i = nodes.size() - numIsolatedPoints();
return ((float) i)/nodes.size();
}
/**
* Density of matrix
*
* @return (2*(float)noEdges)/(nodes.size()*(nodes.size()-1))
* @throws IllegalStateForMatrixException
*/
@Override
float density() throws IllegalStateForMatrixException {
return (2 * (float) noEdges)/(nodes.size() * (nodes.size() - 1));
}
/**
* Print out the adjacency matrix
*/
void print() {
System.out.println(adjM);
}
}
您正在捕捉異常,然後才能退出您的方法。或者你的實際問題是什麼? –
歡迎來到SO。一些建議,閱讀[問],發佈[mcve],並解釋「有問題」的含義。謝謝 – OldProgrammer
你的問題怎麼樣?你想要的方式是什麼? – victor