繁体   English   中英

Java:数组仅显示最后一个元素

[英]Java: Array only prints last element

我已经在这项工作上停留了几个小时,我无法弄清楚。 我创建了一个艺术家数组,其大小由一个变量定义,随着添加更多艺术家的增加,该变量也会增加。 如果我设置artist[] artistList = new artist[totalArtist]; ,我得到arrayoutofbounds或只是空白输出,所以执行artist[] artistList = new artist[1+totalArtist]; 到目前为止为我工作,至少给了我一个输出。 任何改善都会很好

这是我的代码片段:

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;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

       for(artist e : artistList)
           System.out.println(e);

我的主要问题是:在for循环中,我可以创建一个新艺术家,并将其放入artistList数组中。 我也能够打印每个元素。 但是,在try-catch之外,它仅将最后一个元素打印一次。 我不明白自己在做什么错。

请不要建议数组列表,因为如果我能够完成此任务,我显然会这样做。

尝试这样的事情:

public static void main(String[] args)
{
    // create an array for artists
    artist[] artistList = new artist[0];

    // open the original file
    try {
        final File file = new File("p1artists.txt");
        final Scanner input = new Scanner(file);

        while (input.hasNextLine())
        {
            final int id = input.nextInt();
            final String name = input.next();
            // Skip to next line...
            input.netxLine();

            // Create a ne array, with one more element
            final artist[] newList = new artist[artistList.length + 1];
            // Copy the element references to the new array
            System.arraycopy(artistList, 0, newList, 0, artistList.length);
            // create a new artist and put it in the array
            newList[artistList.length] = new artist(id, name);
            // Replace old array with new one
            artistList = newList;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

   for(artist e : artistList)
        System.out.println(e);
}

您的数组仅打印一个元素,因为尝试写入位置2+会得到异常,这会退出try块,并跳转到foreach循环。

数组中只有一个元素

int totalArtist = 0;
artist[] artistList = new artist[1+totalArtist]; // = new artist[1];

由于您无法使用列表,因此可以

  1. 读取文件的行数, 然后创建数组。 Java文件中的行数
  2. 提示要从文件中读取的艺术家数量
  3. 通过自己创建一个具有较大数组的新数组并复制元素来模拟列表

您的artistList.length始终为1。因此,您始终在更新第一个也是唯一的元素。 数组不能动态扩展。 要实现您想要的目标,请考虑ArrayList或LinkedList,具体取决于您如何预见消费结果列表中项目的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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