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