- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个应用许多计算语言学原理的程序。我现在的问题是下面的一段代码形成了一个“灵活化两个定义”的方法。也就是说,它比较同一个词的两个不同定义,并且在每个定义中将添加空格或空格,以便稍后使用更改后的定义(添加空格)。
假设我们有以下两个定义,定义了术语“自由落体”。
1) Free fall descent of a body subjected only to the action of gravity.
2) Free fall movement of a body in a gravitational field under the influence of gravity
有一个单词列表称为非索引字表,其中包含单词:“of”、“a”、“in”、“to”和“under”。在此过程之后,定义中的每个单词也包含在非索引字表中,必须对应于一个空格或另一个定义的另一个非索引字表单词。所以在执行这样的过程之后,前面的定义,在两个不同的列表中表示,应该是这样的:
1) Free fall descent of a body ____ ____ subjected only to the action of gravity.
2) Free fall movement of a body in a gravitational field under the influence of gravity.
我为此编写的代码如下:
[...]
String[] sList = STOPLIST.split(" "); //this is the stoplist
String[] definition1 = defA1.split(" "); //this is the array of words of the first definition
String[] definition2 = defA2.split(" "); //this is the array of words of the second definition
List<String> def1 = new ArrayList<String>();
List<String> def2 = new ArrayList<String>();
List<String> stopList = new ArrayList<String>();
for(String word : definition1){
def1.add(word); //I transform arrays into lists this way because I used to think that using .asList() was the problem.
}
for(String word : definition2){
def2.add(word);
}
for(String word : sList){
stopList.add(word);
}
int mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); //here mdef will have the value of the lenght of the shortest definition, and we are going to use the value of mdef to iterate later on.
for(int i = 0; i < mdef; i++){
if (stopList.contains(def1.get(i))) { //here I check if the first word of the first definition is also found in the stoplist.
if (!stopList.contains(def2.get(i))) { //If the word of def1 previously checked is in the stoplist, as well as the corresponding word in the second definition, then we won't add a " "(blank) space in the corresponding position of the second definition.
def2.add(i , " "); //here I add that blank space, only if the stoplist word in def1 corresponds to a non-stoplist word in def2. Again, we do this so the stoplist word in def1 corresponds to a blank space OR another stoplist word in def2.
if(mdef == def2.size())
mdef++; //In case the shortest definition is the definition to which we just added spaces, we increment mdef++, because that space added increases the length of the shortest definition, and to iterate in this recenlty extended definiton, we have to increment the index with which we iterate.
}
} else if (stopList.contains(def2.get(i))) { //this else if does the same than the previous one, but checks for the second definition instead of the first one. And adds blanks to def1 instead of def2 if necessary.
if (!stopList.contains(def1.get(i))) {
def1.add(i , " ");
if(mdef == def1.size())
mdef++;
}
}
}
[...]
现在,如果您仔分割析代码,您会发现并非最长列表中的所有单词都会被检查,因为我们使用最短定义的长度作为索引来迭代定义。这很好,不需要检查最长定义的剩余单词,它们将对应于另一个定义的空空格(以防列表在添加空格后最终不具有相同的长度,正如前面的例子所示)。
现在,经过解释,问题如下:运行主类后,调用包含前面代码的方法,弹出运行时异常:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:571)
at java.util.ArrayList.get(ArrayList.java:349)
at main2.main(main2.java:75)
我不明白为什么它发现任何列表都是“空的”。我尝试了很多方法来解决它,我希望我能给出一个很好的解释。
如果我将 mdef 分配给最长的尺寸而不是最短的尺寸,这可能会有所帮助,即:
int mdef = (def1.size() >= def2.size()) ? def1.size() : def2.size();
错误变为:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
at java.util.ArrayList.rangeCheck(ArrayList.java:571)
at java.util.ArrayList.get(ArrayList.java:349)
at asmethods.lcc.turnIntoFlex(lcc.java:55)
at asmethods.lcc.calLcc(lcc.java:99)
at main2.main(main2.java:73)'
其中 lcc 是包含方法 turnIntoFlex 的类,该方法包含我正在展示的代码段。 “turnIntoFlex”的第55行对应于循环的第一行,即:
if (stopList.contains(def1.get(i))) { [...]
评论:defA1 和 defA2 的值分别是定义。即 def1 和 def2,最初是列表,其中每个单独的元素都是一个单词。我无法通过打印这些列表来检查它们是否被填充,因为 indexoutofboundsexception 在循环开始的那一刻弹出。但是,我确实打印了 mdef、def1.size() 和 def2.size() 的大小值,结果结果为 13 或 15,表明在“for”循环开始之前没有列表为空.
mdef++ 是我最近添加的东西,并不是为了完全解决这个特定问题,但在我添加 mdef++ 部分之前错误一直在弹出。正如我所解释的,目的是在扩展最短列表时(但仅在扩展短列表时)增加 mdef++,因此我们遍历短列表的所有单词,而不是更多。
最佳答案
您遇到的问题是增量。试试这个:
for(int i = 0; i < mdef; i++){
if (stopList.contains(def1.get(i))) { //here I check if the first word of the first definition is also found in the stoplist.
if (!stopList.contains(def2.get(i))) { //If the word of def1 previously checked is in the stoplist, as well as the corresponding word in the second definition, then we won't add a " "(blank) space in the corresponding position of the second definition.
def2.add(i , " "); //here I add that blank space, only if the stoplist word in def1 corresponds to a non-stoplist word in def2. Again, we do this so the stoplist word in def1 corresponds to a blank space OR another stoplist word in def2.
mdef=Math.min(def2.size(),def1.size);
}
} else if (stopList.contains(def2.get(i))) { //this else if does the same than the previous one, but checks for the second definition instead of the first one. And adds blanks to def1 instead of def2 if necessary.
if (!stopList.contains(def1.get(i))) {
def1.add(i , " ");
mdef=Math.min(def2.size(),def1.size);
}
}
}
关于java - 遍历列表时出现奇怪的 outOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548279/
我有一个 ArrayIndexOutOfBoundsException 问题,它总是出现在我的程序中。我怎样才能进入 try{} ? @Override public Object pop()
这个问题已经有答案了: Are fields initialized before constructor code is run in Java? (5 个回答) 已关闭 7 年前。 我(相对)是j
do{ System.out.println("inside do"); for (int i = 0; i < i2; i++) { Str
我正在为学校做一个项目,我们必须多次旋转一个立方体,并找到从立方体收到的数字的最长运行。我几乎完成了代码,它符合要求,但每当我运行它时,我都会遇到同样的错误: Exception in thread
我正在编写一个应用许多计算语言学原理的程序。我现在的问题是下面的一段代码形成了一个“灵活化两个定义”的方法。也就是说,它比较同一个词的两个不同定义,并且在每个定义中将添加空格或空格,以便稍后使用更改后
我尝试在 Symfony 2 中创建一个自定义身份验证提供程序,只是为了好玩。我关注这个文档 http://symfony.com/doc/current/cookbook/security/cust
假设我有一个方法接受 0 到 x 之间的值。如果参数大于 x 或小于零,我想抛出异常。 OutOfBoundsException 是抛出的合理异常吗? http://php.net/manual/en
嘿,我有一个问题,为什么我的程序会抛出 ArrayIndextOutofBounds 异常我在互联网上到处查看并浏览了代码,但我知道我遗漏了一些东西。这是我第一个实现了堆栈的程序,坦率地说,我并不完全
我正在尝试创建一个 RecyclerView,它根据 Firebase 中的数据填充 CardView。当 Firebase 中没有数据时,我收到 IndexOutOfBoundsException,
我正在编写一个程序来删除单词中的重复字母,但它在第 22 行显示越界错误,我无法弄清楚问题是什么。代码如下 import java.io.*; public class p23 { publi
我的代码有问题。我正在编写一个必须管理每周深蹲训练的程序。可以记录之前的训练或安排一整周的训练。在第二种情况下,用户必须输入每天的深蹲次数和他认为的难度。我用一个有 3 种声音(简单、中等、困难)的选
我正在使用 ListView 来呈现类似 GridView 的 Activity 。存在 OutOfBoundsException。这是日志。 03-21 11:47:13.106: E/Androi
我收到以下错误: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 86, Size: 86 at java
如果对象 LimitIterator 为空,我如何编写一个不运行下面的 foreach 的条件? $numbers = array(5, 19, 8, 35, 50); $iterator = new
PHP 定义了两个 SPL无效键的异常(exception)情况: OutOfRangeException: Exception thrown when an illegal index was re
我有以下代码: @SuppressWarnings("resource") public void changeSkin(String name){ try { URL uui
这是我实现埃拉托斯特尼筛法的函数, void solve() throws IOException { int n = 200000; ArrayList primes = new A
这个问题已经有答案了: Array index out of bounds when getting arguments (4 个回答) 已关闭 7 年前。 我正在处理一项作业,但不允许我使用扫描仪。
我不是专家,不知道为什么会越界 public class ParticipantIndexComparator implements Comparator { final List parti
对于我的 CompSci 类(class),我们正在为我们的聊天机器人项目制作一个 Would You Rather? 功能。 String.split() 方法对此很有效,但如果我们不用它也能做到,
我是一名优秀的程序员,十分优秀!