如何从数组

时间:2015-07-29 14:47:31

标签: 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或其他什么东西?

4 个答案:

答案 0 :(得分:1)

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

array = ArrayUtils.removeElement(array,element)

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

答案 1 :(得分:1)

您可以使用System.arraycopy(src, srcPos, dest, destPos, length);方法。

此方法不会创建新数组。它将修改现有数组。 请参阅以下代码:

String[] arr = {"Anil","Stack","Overflow","Reddy"};
  int size = arr.length;
  int index = 2;
  System.arraycopy(arr, index+1, arr, index, size-index-1); // modified the existing array, New Array will not create.
  arr[--size] = null;
  System.out.println(Arrays.toString(arr)); 

答案 2 :(得分:0)

以下是工作代码:在您的文件上进行测试

只需更改文件路径即可。

问题是'\ t',它是tab而不是'\\ t',它是一个字符串'\ t'而不是一个标签。

public static void main(String[] args) {

// The name of the file to open.
String fileName = "D:\\64416.txt";

// 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);

    FileWriter fw = new FileWriter("D:\\item2.txt"); //for Writing the file

    while ((line = bufferedReader.readLine()) != null) {
        String[] values = line.split("\t");
        List<String> list = new ArrayList<String>(Arrays.asList(values));
        list.remove(2);  // removing value from index 2
        for (String string : list) {
            fw.write(string + "\t"); //tab to be included
        }
        fw.write("\n");
        System.out.println(values[2]);
    }

    // Always close files.
    bufferedReader.close();
    fw.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();
}
}

答案 3 :(得分:0)

您知道要移除哪个元素,因此声明一个新数组,其长度比String.split()

中的原始数组小1个元素

然后使用System.arraycopy(),将要保留的元素从原始数组复制到新数组中。

public static void main(String[] args) throws Exception {
    String data = "item = 0\tDwarf_remains\tThe_body_of_a_Dwarf_savaged_by_Goblins.\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0";

    System.out.println("Before: ");
    System.out.println(data);
    System.out.println();

    String[] pieces = data.split("\t");
    String[] newArray = new String[pieces.length - 1];
    // Copy index 0 & 1
    System.arraycopy(pieces, 0, newArray, 0, 2);
    // Skip index 2, and copy the rest
    System.arraycopy(pieces, 3, newArray, 2, pieces.length - 3);

    System.out.println("After: ");
    System.out.println(String.join("\t", newArray));
}

结果:

Before: 
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

After: 
item = 0    Dwarf_remains   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

否则,您可以使用for循环将原始数组中的元素复制到新数组中,跳过新数组中不需要的任何元素。

int newArrayIndex = 0;
for (int i = 0; i < pieces.length; i++) { 
    // Skip index 2       
    if (i == 2) {
        continue;
    }

    newArray[newArrayIndex] = pieces[i];
    newArrayIndex++;
}