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

标签 java arrays

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

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)

数组 = 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/

相关文章:

java - 将输入拆分为 vector

Java : a method to do multiple calculations on arrays quickly

java - 如何使用两个for循环制作空心盒子

c# - 在 Array 类上使用 Linq 扩展

C 获取数组长度函数

javascript - 迭代 JSON 格式的对象数组并修改和扩展它,最好使用 Underscore.js

java - Spring 启动 + SSL : Consuming Too much CPU

java - 无法从 Firebase(Cloud Firestore 数据库)获取数据

arrays - 无法将类型 '__NSSingleEntryDictionaryI' (0x10d249f78) 的值转换为 'NSArray'

javascript - 如何对具有相似键的数据进行分组?