有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:匹配数组元素并删除

我在这个计划中有几件事我需要做。下面,我在读两个文件,p1artists是原始文件,我必须将每一行读入一个数组并创建一个新的Artister对象。p1artists中的每一行都以艺术家ID(例如1-60)及其姓名开头。第二个文件p2changes有一个文件,指定要删除的艺术家或要添加的新艺术家。要添加的艺术家只是用“A”标记,后面跟着他们的名字。要删除的艺术家后面跟着一个“D”,只有艺术家ID

我注意到的第一个问题是,当我在程序结束时打印artistList数组时,它只显示数组中的最后一个元素/艺术家。我希望能够打印整个列表。我怎么能做到呢

我的第二个问题是在第二个try catch中,我尝试删除艺术家。我无法使用if-else语句。如有任何帮助或提示,将不胜感激

到目前为止我的计划

public static void main(String[] args) {
    //initialize total artists and id
    int totalArtist = 0;
    int newID = 0;

    //create an array for artists
    artist[] artistList = new artist[1 + totalArtist];

    //open the original file
    try {
        File file = new File("p1artists.txt");
        Scanner input = new Scanner(file);
        while (input.hasNextLine()) {
            int id = input.nextInt();
            String name = input.next();

            //create a new artist and put it in the array
            for (int i = 0; i < artistList.length; i++) {
                artistList[i] = new artist(id, name);
                totalArtist++;

            }
            //increment to a newID
            newID = id + 1;

        }
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }



    //open the file to be added/remove from original
    try {
        File file = new File("p2changes.txt");
        Scanner input = new Scanner(file).useDelimiter("\t|\r\n");
        while (input.hasNext()) {
            //read the first element of the line and check if it is A or D
            if (input.next().equals("A")) {
                String name = input.next();

                //add a new artist to the arraylist and increment newID
                for (int i = 0; i < artistList.length; i++) {
                    artistList[i] = new artist(newID, name);
                    newID++;
                    totalArtist++;

                }
            } else {
                int id = input.nextInt();
                System.out.println(id);
                if (input.hasNext()) {
                   //go through array and find artist to be deleted
                    for (int i = 0; i < artistList.length; i++) {
                        if(artistList[i].getID() == id)
                            //remove artist from array list

                            //decrease total artists
                            totalArtist--;
                        else
                            System.out.println("Not working");

                    }
                }
            }

        }
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

一个片段看起来像

1   Acconci
2   Ames
3   Aserty
4   Baron

p2changes看起来是什么样的一个片段

A   Reed
A   Rissman
D   3
A   Rogers

输出会是什么样子

1  Acconci
2  Ames
4  Baron
5  Reed
6  Rissman
7  Rogers

共 (1) 个答案

  1. # 1 楼答案

    //initialize total artists and id
    int totalArtist = 0;
    int newID = 0;
    
    //create an array for artists
    artist[] artistList = new artist[1 + totalArtist];
    

    我想您可能对数组大小有问题。尽管在添加或删除艺术家时,您会使用TotalArtister++或for循环,但artistList数组的大小始终设置为1,并会在程序执行时根据更新的TotalArtister编号进行更改

    ArrayIndexOutOfBound有错误吗

    我想你也知道这一点,但是如果你想通过每个元素和println()每个元素为循环打印整个数组