gpt4 book ai didi

java - 如何从数组中删除一个值

转载 作者:行者123 更新时间:2023-11-30 08:48:53 25 4
gpt4 key购买 nike

我想从文本文件中获取的数组中删除第三个值。我的文本文件如下所示:

item = 0    Dwarf_remains   The_body_of_a_Dwarf_savaged_by_Goblins. 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
item = 1 Toolkit Good_for_repairing_a_broken_cannon. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 2 Cannonball Ammo_for_the_Dwarf_Cannon. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 3 Nulodion's_notes It's_a_Nulodion's_notes. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 4 Ammo_mould Used_to_make_cannon_ammunition. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
item = 5 Instruction_manual An_old_note_book. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

然后我想删除所有这些东西:

The_body_of_a_Dwarf_savaged_by_Goblins.
Ammo_for_the_Dwarf_Cannon.
It's_a_Nulodion's_notes.
Used_to_make_cannon_ammunition.
An_old_note_book.

我现在正在使用这段代码来获取数组

import java.io.*;

public class Main {
public static void main(String [] args) {

// The name of the file to open.
String fileName = "Items.cfg";

// This will reference one line at a time
String line = null;

try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
String[] values = line.split("\\t");
System.out.println(values[2]);
}

// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}

当 java 删除所有这些时,是否可以将更新的文件保存在另一个文件中,例如 Items2.cfg 或其他文件?

最佳答案

例如这里类似于此?我复制了答案以便于访问。 Removing an element from an Array (Java)

array = ArrayUtils.removeElement(数组, 元素)

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html

关于java - 如何从数组中删除一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31703864/

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