你好,我一直在尝试将一个 String
添加到一个 String[]
中。这是我的代码:
static String[] ipList = {"127.0.0.1", "173.57.51.111", "69.696.69.69"};
@Override
public void actionPerformed(ActionEvent e) {
String newIpGet = textfield.getText();
try {
for ( int i = 0; i < Main.ipList.length; i++){
Main.ipList[i+1] = newIpGet.toString(); // <---- *****
Main.write(Main.ipList[i]);
}
} catch (IOException e1) {
e1.printStackTrace();
}
Main.amountOfIps = Main.amountOfIps + 1;
System.out.println("Text Entered!");
System.out.println("There are now " +Main.ipList.length + " Ips.");
textfield.setVisible(false);
label.setVisible(true);;
}
但是,我一直收到 java.lang.ArrayIndexOutOfBoundsException
的错误提示,因为它不允许我创建任何新的 String
。 我不能修改我的声明 ipList[]
,因为这需要进行很多修改,我该怎么办?
ArrayList
,它本质上会执行这个操作。 - MadProgrammer