gpt4 book ai didi

java - 将最后一个元素的信息添加到 ArrayList 的末尾

转载 作者:行者123 更新时间:2023-12-01 14:32:39 27 4
gpt4 key购买 nike

我的问题很简单。我正在尝试使用如下行将 arrayList 的最后一个元素添加到该数组列表的末尾:

   ArrayList.add(ArrayList.get(ArrayList.size()));

但是我得到的是一个indexOutOfBoundsException。

有没有办法在不引发异常的情况下做到这一点?快速回答。该行不包含在任何循环中。 arrayList 有大小。我已经用 ArrayList.size()-1 尝试过,但没有成功,这里是它包含的完整代码块。

public class SaveButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(".");
int status = fileChooser.showSaveDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
String fileName = selectedFile.getPath();
File imageFolder = new File(fileName);
imageFolder.mkdir();

//tokenizes the path into an arraylist
String delims = "/";
ArrayList<String> fileTokensList = new ArrayList<>();
String[] fileTokens = fileName.split(delims);
fileTokensList.addAll(Arrays.asList(fileTokens));

//adds the name of the current image
fileTokensList.add(fileTokensList.get(fileTokensList.size()));

//rebuilds all the tokens into one string path
StringBuilder builder = new StringBuilder();
for (int s = 0; s < fileTokensList.size(); s++) {
if (s == 0) {
builder.append(fileTokensList.get(s));
} else {
builder.append("/");
builder.append(fileTokensList.get(s));
}
}
String textFileName = builder.toString();
System.out.println(textFileName);
try {
File saveFile = new File(textFileName);
FileWriter fileWriter = new FileWriter(saveFile);
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
for (int i = 0; i < Driver.vehicleList.size()+1; i++) {

//writes the text file (one entry per line
bufferedWriter.write(Driver.vehicleList.get(i).getPrice());
bufferedWriter.write("\t");
bufferedWriter.write(Driver.vehicleList.get(i).getYear());
bufferedWriter.write("\t");
bufferedWriter.write(Driver.vehicleList.get(i).getMake());
bufferedWriter.write("\t");
bufferedWriter.write(Driver.vehicleList.get(i).getModel());
bufferedWriter.write("\t");
bufferedWriter.write(Driver.vehicleList.get(i).getComments());
bufferedWriter.newLine();

//adds the name of the current image
fileTokensList.add(Integer.toString(i));

//rebuilds all the tokens into one string path
StringBuilder imagePathBuilder = new StringBuilder();
for (int s = 0; s < fileTokensList.size(); s++) {
if (s == 0) {
imagePathBuilder.append(fileTokensList.get(s));
} else {
imagePathBuilder.append("/");
imagePathBuilder.append(fileTokensList.get(s));
}
}
String imageFileName = builder.toString();

//resaves the picture to the same place
Path FROM = Paths.get(Driver.vehicleList.get(i).getCarPicture());
Path TO = Paths.get(imageFileName);
//overwrite existing file, if exists
CopyOption[] options = new CopyOption[]{
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES
};
Files.copy(FROM, TO, options);
}
}
} catch (IOException IO) {
System.out.println("Error writing to file");
}
JOptionPane.showMessageDialog(null, "All your information has been saved");
}
}
}

这段代码的目的是将文件保存到某个位置,我对 token 做了很多困惑并重建了该路径。我是否花了很长时间才找到一个简单的解决方案,或者我做得对吗?

最佳答案

如果 index 值介于 0 和 Array.size() - 1 之间,

ArrayList.get(int index) 将起作用。由于您要发送Array.size(),因此您将到达数组的无效位置,从而出现异常。

如何解决这个问题?好吧,您应该发布您要添加的内容,然后您就会得到答案。

关于java - 将最后一个元素的信息添加到 ArrayList 的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16739784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com