简体   繁体   中英

Deleting an entry from an array in Java

It's more complicated than the title. I'm using a loop to write to a file, then I will read the file using Scanner class and File class, after that I want to store the data that the classes have read into an array. After that, the user will choose one of the entries which are inside the array to delete.

I know how to declare the array and everything, but I'm stuck at how to store the file's info into the array, and then the deletion of one entry (L102 for example), here's the code: PS please, after running the code, copy Pats file to the C: directory.

package lecture;
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class Lecture{

public static void main (String args[]) throws IOException
{

    PrintWriter f0 = new PrintWriter(new FileWriter("Pats.txt"));
    int n=0;

    while(n<15)
        {

        int L=1;
        n++;
        f0.print("L"+L+"0"+n+"  ");
        System.out.println("L"+L +" "+n);

        L=L+1;
        f0.print("L"+L+"0"+n+"  ");
        System.out.println("L"+L +" "+n);

        L=L+1;
        f0.print("L"+L+"0"+n+"  ");
        System.out.println("L"+L +" "+n);

        }

        File Plots = new File("C:\\Pats.txt");
        Scanner ReadFile = new Scanner(Plots);
            while(ReadFile.hasNext())
            {

                String str = ReadFile.nextLine();
                System.out.println(str);

            }
         ReadFile.close();



f0.close();
}
}

I'm not sure if your line of thinking is the optimum, but inside your while loop, you can have something along the lines of:

while(ReadFile.hasNext())
{

     String str = ReadFile.nextLine();
     System.out.println(str);
     lines.add(str);
}

Where lines is an ArrayList declared outside of the loop.

To delete from this, all you have to do is have the user select the index , and do lines.remove(index) or have the user select a String , in which case you can do lines.remove(string) .

As I understood your question, you want to read your data from file to an array and then delete any element from that array. So you can do it as follows,

  • Read data to an array

Read data from your file line by line into StringBuilder

File file = new File("Pats.txt");
Scanner sc = new Scanner(file);
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
    sb.append(sc.nextLine());
}
sc.close();

Split data and assign result to an array

final String readStr = sb.toString();
String[] array = readStr.split("  ");
  • Delete an element from array

Since we cannot resize java arrays and still you wanted to use an array, you can use Apache Commons , Lang component . Download jar from here . See javadoc here .

array = ArrayUtils.removeElement(array, "L102");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM