我有(在这个例子中)两个ArrayList
s。明文字母表和加密字母表。我想从明文字母表中取一个字母,并从加密字母表的相应索引中得到该字母。但是,即使条目存在,它始终将索引返回为-1。Java - arraylist1.get(arraylist2.indexOf(nextCharacter))返回-1
public class Rotor {
String currentIndex;
private ArrayList rotorType = new ArrayList();
private ArrayList control = new ArrayList();
private final char[] alpha = new char[]{
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'};
static char[] I = new char[]{
'E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q',
'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H',
'X', 'U', 'S', 'T', 'A', 'I', 'B', 'R',
'C', 'J'};
public Rotor(char[] rotor) {
for (int i = 0; i < 25; i++) {
rotorType.add(rotor[i]);
control.add(alpha[i]);
}
}
public void convert(String nextCharacter) {
currentIndex = String.valueOf(rotorType.get(control.indexOf(nextCharacter)));
}
}
什么会导致此返回索引-1?
“char”值(或者说“Character”值)不是字符串。 –
请勿使用原始类型。 –
请创建一个[MVCE](http://stackoverflow.com/help/mcve)。 – Turing85