0

如果我在我的 arrayList 中搜索第 5 项,我还想获得第 4 项和第 6 项。最终的 if 语句中提供了此代码,并定义为 (i - 1) 和 (i + 1)。到目前为止,这是我的代码:

import java.util.ArrayList;

public class PlanetsList {
public static void main(String args[]) {

    ArrayList<String> planets = new ArrayList<String>();
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranis", "Neptune", "Pluto"};

    for (int i = 0, n = names.length; i < n; i++) {
        planets.add(names[i]);
        String value = (String) planets.get(i);

        if(value.contains("Mars")) {
            String newNum = value.replace(value, "Red planet ");
            planets.set(i,newNum);     
        }
        if(value.contains("Uranis")) {
            String wordBefore = (String) planets.get(i-1);
            String wordAfter = (String) planets.get(i+1);
            String newNum = value.replace(value, "Uranus ");
            planets.set(i,newNum);
            System.out.println("This is the word before " + wordBefore);
            System.out.println("This is the word after " + wordAfter);
            planets.remove(i-1);  
        }
    }
    System.out.println(planets);
}
}

有了这个我得到一个 indexoutofbounds 异常,这显然是由最终 if 语句中的 wordAfter 行引起的,并且因为 for 循环没有遍历整个 arrayList。最后的 if 语句不需要和另一个 if 语句在同一个 for 循环中,但如果它在不同的循环中,replace 方法必须能够将被替换的单词放回正确的位置。

问候

4

6 回答 6

2

此行导致问题。

 String wordAfter = (String) planets.get(i+1);

因为最大索引是 i。

如果您按照以下方式进行检查会更好。

public static void main(String args[]) {

    ArrayList planets = new ArrayList();
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranis", "Neptune", "Pluto"};

    for (int i = 0, n = names.length; i < n; i++) {
        planets.add(names[i]);

    }
    for (int i = 0, n = names.length; i < n; i++) {

        String value = (String) planets.get(i);

        if(value.contains("Mars")) {
            String newNum = value.replace(value, "Red planet ");
            planets.set(i,newNum);     
        }
        if(value.contains("Uranis")) {
            String wordBefore = (String) planets.get(i-1);
            String wordAfter = (String) planets.get(i+1);
            String newNum = value.replace(value, "Uranus ");
            planets.set(i,newNum);
            System.out.println("This is the word before " + wordBefore);
            System.out.println("This is the word after " + wordAfter);   
        }
    }
    System.out.println(planets);
}
于 2013-01-24T09:07:30.403 回答
2

完成将字符串添加到您的列表中,然后再次尝试迭代列表以获取您的逻辑。您正在尝试get(i+1)何时i将最大值添加到您的列表中。

于 2013-01-24T08:59:58.913 回答
1

You can use Arrays.asList to convert your array into a List and then iterate through the list to do your processing. You should check anyway that the index of wordBefore and wordAfter are within the List limits.

于 2013-01-24T09:12:03.300 回答
1

我认为这将解决您的问题

你的代码给出了异常

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at PlanetsList.main(PlanetsList.java:20)

这是一个工作:

import java.util.ArrayList;

public class PlanetsList {
public static void main(String args[]) {

ArrayList planets = new ArrayList();
String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranis", "Neptune", "Pluto"};

for (int i = 0, n = names.length; i < n; i++) {
planets.add(names[i]);
}
for (int i = 0, n = names.length; i < n; i++) {
String value = (String) planets.get(i);

if(value.contains("Mars")) {
String newNum = value.replace(value, "Red planet");
planets.set(i,newNum);     
}
 if(value.contains("Uranis")) {
String wordBefore = (String) planets.get(i-1);
String wordAfter = (String) planets.get(i+1);
String newNum = value.replace(value, "Uranus");
planets.set(i,newNum);
System.out.println("This is the word before " + wordBefore);
System.out.println("This is the word after " + wordAfter);   
 }
}
System.out.println(planets);
}
}

我对您的代码进行了两项更改:

  1. 采取了两个 for 循环
  2. spaces从以下两行中删除

    String newNum = value.replace(value, "Red planet ");
    String newNum = value.replace(value, "Uranus ");
    
于 2013-01-24T09:04:55.330 回答
1

planets.add(names[i]);...在这一行中,只有你正在向 arraylist 添加一个值。此时,你的 arraylist 的大小是 i。不增加 i 或向 arraylist 添加任何值,你正在尝试在 i+1 处访问元素..所以它肯定会抛出 IOB 异常

于 2013-01-24T09:01:38.467 回答
1

我也觉得这个问题不是不言自明的......但是如果您需要在数组列表中的特定项目之前和之后获取元素,您可以这样做。

我的假设是您已经有一个预先填充了值的列表。

String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranis", "Neptune", "Pluto"};

List<String> planets = java.util.ArrayList.asList(names);

if (planets.contains("Uranis") ) {
   int currentIndex = planets.indexOf("Uranis");       
   String previousItem = planets.get(currentIndex-1);
   // To ensure that we have an element next to the current one. 
   if (currentIndex < (planets.size() - 1 )) {
    String nextItem = planets.get(currentIndex+1); 
   }
}

我希望这能帮到您

~拉吉什

于 2013-01-24T09:16:15.960 回答