簡體   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