- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我很难抛出 IllegalStateForMatrixException。如果有人可以帮助我,我将不胜感激。这是我的教授告诉我要做的,但实际上我仍然遇到问题。这是他给我的提示......
当调用具有 0 个边的 getNumberOfEdges 时,应引发 IllegalStateForMatrixException。当调用具有 0 个边的 getNumberOfEdges 时,应引发 IllegalStateForMatrix。您需要做的就是检查矩阵没有正值。如果它没有抛出 IllegalStateForMatrix 异常。
您还可以将 boolean 值设置为 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);
}
}
最佳答案
How do I throw and catch the IllegalStateForMatrix properly. Where would I properly catch the exception? –
这里 IllegalStateForMatrixException
表示在对象上调用了一个操作,但该对象不处于接受此操作所需先决条件的状态。
您想要获得许多边,但没有矩阵?这没有任何意义。
这是一个重要的问题,因此您必须引发异常直到引发异常的方法的调用者。
除此之外,这些方法还声明抛出 IllegalStateForMatrixException
。
因此,如果检查了异常(不是 RuntimeException 的子异常),则客户端必须处理它。
在您的代码中,有两个方法抛出异常。
在这里,你可以做好事:
@Override
int getNumberOfEdges() throws IllegalStateForMatrixException {
if (nodes.size() <= 0)
throw new IllegalStateForMatrixException("Error");
return noEdges;
}
您将异常抛出给必须处理它的客户端。
那里:
void addEdge(String fromNode, String toNode, int weight)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
if ( b == false)
throw new IllegalStateForMatrixException("Exception");
}
...
}
catch (IllegalStateForMatrixException e) {
System.out.println("Exception found");
}
...
}
当您在方法中引发异常时,您不允许将异常传播给调用者,但您也捕获了它。
与第一种情况一样,您不需要 try/catch 语句。
关于java - 抛出异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812460/
问题很简单:我正在寻找一种优雅的使用方式 CompletableFuture#exceptionally与 CompletableFuture#supplyAsync 一起.这是行不通的: priva
对于 Web 服务,我们通常使用 maven-jaxb2-plugin 生成 java bean,并在 Spring 中使用 JAXB2 编码。我想知道如何处理 WSDL/XSD 中声明的(SOAP-
这个问题已经有答案了: Array index out of bound behavior (10 个回答) 已关闭 8 年前。 我对下面的 C 代码感到好奇 int main(){
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
嘿 我尝试将 java 程序连接到 REST API。 使用相同的代码部分,我在 Java 6 中遇到了 Java 异常,并且在 Java 8 中运行良好。 环境相同: 信任 机器 unix 用户 代
我正在尝试使用 Flume 和 Hive 进行 Twitter 分析。为了从 twitter 获取推文,我在 flume.conf 文件中设置了所有必需的参数(consumerKey、consumer
我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台
假设我使用 blur() 事件验证了电子邮件 ID,我正在这样做: $('#email').blur(function(){ //make ajax call , check if dupli
我这样做是为了从 C 代码调用非托管函数。 pCallback 是一个函数指针,因此在托管端是一个委托(delegate)。 [DllImport("MyDLL.dll")] public stati
为什么这段代码是正确的: try { } catch(ArrayOutOfBoundsException e) {} 这是错误的: try { } catch(IOException e) {} 这段
我遇到了以下问题:有导出函数的DLL。 代码示例如下:[动态链接库] __declspec(dllexport) int openDevice(int,void**) [应用] 开发者.h: __de
从其他线程,我知道我们不应该在析构函数中抛出异常!但是对于下面的例子,它确实有效。这是否意味着我们只能在一个实例的析构函数中抛出异常?我们应该如何理解这个代码示例! #include using n
为什么需要异常 引出 public static void main(String[
1. Java的异常机制 Throwable类是Java异常类型的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,他才是一个异常对象,才能被异常处理机制识别。JDK中内
我是 Python 的新手,我对某种异常方法的实现有疑问。这是代码(缩写): class OurException(Exception): """User defined Exception"
我已经创建了以下模式来表示用户和一组线程之间的关联,这些线程按他们的最后一条消息排序(用户已经阅读了哪些线程,哪些没有): CREATE TABLE table(user_id bigint, mes
我正在使用 Python 编写一个简单的自动化脚本,它可能会在多个位置引发异常。在他们每个人中,我都想记录一条特定的消息并退出程序。为此,我在捕获异常并处理它(执行特定的日志记录操作等)后引发 Sys
谁能解释一下为什么这会导致错误: let xs = [| "Mary"; "Mungo"; "Midge" |] Array.iter printfn xs 虽然不是这样: Array.iter pr
在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @With 和一个 @Check 注释。 断开连接后,一切正常。连接后,每次加载页面(任何页面,无论
我尝试连接到 azure 表存储并添加一个对象。它在本地主机上工作得很好,但是在我使用的服务器上我得到以下异常及其内部异常: Exception of type 'Microsoft.Wind
我是一名优秀的程序员,十分优秀!