Java 如何从数组中删除值
我想从文本文件中获取的数组中删除第三个值。 我的文本文件如下所示:Java 如何从数组中删除值,java,arrays,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
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或其他文件?例如,与此类似?我把答案抄了一遍,以便查阅。 array=ArrayUtils.removeElement(数组,元素)
您可以使用
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));
以下是工作代码:在您的文件上进行了测试
只需更改文件路径
问题是“\t”是制表符,而不是“\\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();
}
}
publicstaticvoidmain(字符串[]args){
//要打开的文件的名称。
字符串fileName=“D:\\64416.txt”;
//这将一次引用一行
字符串行=null;
试一试{
//FileReader以默认编码读取文本文件。
文件阅读器文件阅读器=
新文件阅读器(文件名);
//始终将文件读取器包装在BufferedReader中。
缓冲读取器缓冲读取器=
新的BufferedReader(文件阅读器);
FileWriter fw=newfilewriter(“D:\\item2.txt”);//用于写入文件
而((line=bufferedReader.readLine())!=null){
String[]value=line.split(“\t”);
List List=newarraylist(Arrays.asList(values));
list.remove(2);//从索引2中删除值
用于(字符串:列表){
fw.write(字符串+“\t”);//要包括的选项卡
}
fw.写入(“\n”);
System.out.println(值[2]);
}
//始终关闭文件。
bufferedReader.close();
fw.close();
}捕获(FileNotFoundException ex){
System.out.println(“无法打开文件“”+fileName+”);
}捕获(IOEX异常){
System.out.println(“读取文件“+”文件名“+”时出错”);
//或者我们可以这样做:
//例如printStackTrace();
}
}
您知道要删除哪个元素,因此从字符串.split()
然后使用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));
}
int newArrayIndex = 0;
for (int i = 0; i < pieces.length; i++) {
// Skip index 2
if (i == 2) {
continue;
}
newArray[newArrayIndex] = pieces[i];
newArrayIndex++;
}
结果:
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
循环将元素从原始数组复制到新数组,跳过新数组中不需要的任何元素
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));
}
int newArrayIndex = 0;
for (int i = 0; i < pieces.length; i++) {
// Skip index 2
if (i == 2) {
continue;
}
newArray[newArrayIndex] = pieces[i];
newArrayIndex++;
}
int-newArrayIndex=0;
对于(int i=0;i
我在您的代码中没有看到任何数组。要编写文件,您可以使用Java中的FileWriter
,\t
表示单个选项卡
,\\t
将被视为字符串“\t”
而不是选项卡
有趣的代码,arr[--size]做什么?从未见过这样的语法。从数组中删除一个元素后。我将最后一个元素设为NULL。否则它将接受最后一个值。明白了,我想是的,为什么我们要将它设置为null呢?这有点让我讨厌。@Anilarr[--size]=null
不会减少数组长度;它只会清空数组的最后一个元素。是的,这是正确的,但是您需要添加commons apache org jar。强烈建议下载ApacheCommons,那里有很多有用的工具。我真的不太明白这一点。我是java的初学者,ApacheCommons是以.jar文件的形式提供给您的新方法的可下载集合。它包含许多java默认情况下没有的好扩展。我强烈建议大家阅读一下。这里的评论是针对OP的,请注意,他使用的数组列表与常规数组略有不同。在Java中,\t
表示单个选项卡
,\\t
将被视为字符串“\t”
,而不是选项卡
,谢谢,这很有效。我只在线程“main”java.lang.IndexOutOfBoundsException中得到一个错误异常:在java.util.ArrayList.rangeCheck(未知源代码)在java.util.ArrayList.remove(未知源代码)在main.main(main.java:29)中得到索引:2,大小:1
,它在删除了其中一些内容之后发生。这是我的整个文件:这意味着您试图访问第二个索引,而您的数组大小只有一个,请检查以确保您没有试图访问超出范围的内容。因此有例外。