package com.cp.javapractice;
import java.util.ArrayList;
import java.util.Scanner;
public class Cp {
public static void main(String args[]) {
ArrayList al = new ArrayList();
Scanner s = new Scanner(System.in);
String str = null;
str = new String();
System.out.println("Enter the string which you want to remove the duplicates");
str = s.nextLine();
String arr[] = str.split(" ");
for (int k = 0; k < arr.length; k++) {
al.add(arr[k]);
}
try {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i].equalsIgnoreCase(arr[j])) {
al.remove(j);
}
}
}
System.out.println(al);
}
catch (Exception e) {
System.out.println(e);
}
}
}
我打算从用户中替换特定给定字符串中的重复单词。因此,我使用split方法将给定的字符串与空间分开,并将其放入数组以及arraylist中。循环执行时IndexOutOfBoundsException异常-java
遍历数组并检查条件后,它是相等的,然后我删除了ArrayList中的条件。但是,删除它显示索引超出限制例外。
此代码适用于小数组大小,但在给出大量数组大小时显示异常。 我有问题,而我给与13个字的数组大小的字符串。
这是我的完整代码。
这是一个伟大的时间来学习如何使用调试器! – jdigital
您省略了关键信息:异常堆栈跟踪(完整)以及代码中哪个语句引发异常的指示。请[编辑]您的问题,以包括缺少的信息 –
[IndexOutOfBoundsException]可能的重复(http://stackoverflow.com/questions/4269153/indexoutofboundsexception) – mx0