如何检查字符串是否包含字母表的所有字母?
我正在尝试检查字符串是否包含字母表的所有字母。我创建了一个包含整个字母表的内容。我将字符串转换为char数组,并且我正在迭代字符数组,并且对于内存中删除元素中的每个字符。最后,我试图检查是否为空,以查看是否所有元素都已被删除。这将指示字符串包含字母表的所有字母。ArrayList
ArrayList
Arraylist
不幸的是,代码在我从数组列表中删除元素的if条件中引发错误IndexOutOfBoundsException
List<Character> alphabets = new ArrayList<Character>();
alphabets.add('a');
alphabets.add('b');
alphabets.add('c');
alphabets.add('d');
alphabets.add('e');
alphabets.add('f');
alphabets.add('g');
alphabets.add('h');
alphabets.add('i');
alphabets.add('j');
alphabets.add('k');
alphabets.add('l');
alphabets.add('m');
alphabets.add('n');
alphabets.add('o');
alphabets.add('p');
alphabets.add('q');
alphabets.add('r');
alphabets.add('s');
alphabets.add('t');
alphabets.add('u');
alphabets.add('v');
alphabets.add('w');
alphabets.add('x');
alphabets.add('y');
alphabets.add('z');
// This is the string- I've just put a random example
String str = "a dog is running crazily on the ground who doesn't care about the world";
//Remove all the spaces
str = str.replace(" ", "");
// Convert the string to character array
char[] strChar = str.toCharArray();
for (int i = 0; i < strChar.length; i++) {
char inp = strChar[i];
if (alphabets.contains(inp)) {
alphabets.remove(inp);
}
}
if (alphabets.isEmpty())
System.out.println("String contains all alphabets");
else
System.out.println("String DOESN'T contains all alphabets");