为什么我的Java ArrayList添加了对象,但后来打印为空?

时间:2017-02-20 04:59:06

标签: java arraylist

我有3个类:一个Item类,一个Pantry类(存储项目)和一个ReadFile类(我从文本文件中读取项目)。

我将从文本文件中读取这些项目,然后这些项目将成为我的Item类中的对象,然后我想将对象项目存储在我ArrayList的{​​{1}}中} class。

文本文件如下所示:

Pantry

很简单。 Salami 2 3 4 Fish 1 6 2 Jerky 3 1 6

String -tab- int -tab- int -tab- int

import java.util.Comparator; /** Item class * An object */ public class Item { private String name; private int value; private int weight; private int avail; /** * Item(String name, int value, int weight, int avail) * Initializes attributes */ public Item(String name, int value, int weight, int avail) { this.name = name; this.value = value; this.weight = weight; this.avail = avail; } //Just contains Getters/Setters for each attribute } 上课:

ReadFile

现在是/**ReadFile class * Class that reads data from a .txt file */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFileChooser; public class ReadFile{ private static BufferedReader reader; private static BufferedWriter writer; private static String currentLine; //Following code is from: https://www.youtube.com/watch?v=9VrtranTJnc public static void main(String [] args) throws IOException { File inFile; JButton open = new JButton(); JFileChooser jfc = new JFileChooser(); jfc.setCurrentDirectory(new java.io.File(".")); jfc.setDialogTitle("Knapsack Program"); if (jfc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION) { //end code from https://www.youtube.com/watch?v=9VrtranTJnc inFile = jfc.getSelectedFile(); System.out.println("Input File Chosen: " + inFile); try { reader = new BufferedReader(new FileReader(inFile)); }catch (FileNotFoundException e){ System.out.println("The file was not found"); System.exit(0); } try { Scanner input = new Scanner(inFile); //Add lines from text file to Item object while ((currentLine = reader.readLine()) != null){ String name = input.next() ; int value = input.nextInt(); int weight = input.nextInt(); int avail = input.nextInt(); Item newItem = new Item(name, value, weight, avail); newItem.setName(name); newItem.setValue(value); newItem.setWeight(weight); newItem.setAvail(avail); //Add this new Item to the array of items. Pantry p = new Pantry(); p.addItemToArray(newItem); } reader.close(); input.close(); }catch (IOException e2){ System.out.println("The file was not found"); System.exit(0); } Pantry p2 = new Pantry(); //just for testing p2.printArray(); //just for testing } } 类:

Pantry

我的打印结果如下:

**Pantry class
 * A collection class that stores items into an array
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Pantry  {

    private ArrayList<Item> itemArray = new ArrayList<>();


    /** getItemArray()
     * @return itemArray  Returns the arraylist of items
     */
    public ArrayList<Item> getItemArray() {
        return itemArray;
    }

    /**
     * setItemArray(ArrayList<Item> itemArray)
     * @param itemArray
     */
    public void setItemArray(ArrayList<Item> itemArray) {
        this.itemArray = itemArray;
    }


    /** addItemToArray(Item item)
     * item = an object
     * Add Item to array
     * @return 
     */
    public void addItemToArray (Item item)  {
            itemArray.add(item);


        System.out.println("ADDED: " + itemArray);//just for testing  - looks like it added the items.

        }


    /** toString()
     * @return  One string representing all of list
     */
    public String toString()    {
        String result = "";
        for (int i = 0; i < itemArray.size(); i++)  {
            result += itemArray.toString() + "\n";
        }
        return result;
    }

    public void printArray() {         

        Pantry p = new Pantry();
        System.out.println("Print Array: " + p.getItemArray());
        System.out.println("Print Array string: " + p.toString());
//Both of these print as empty.
        }    

    }

}

任何人都可以帮我看看为什么arraylist打印空?我确定它很简单,但我只是没有看到它。

1 个答案:

答案 0 :(得分:0)

因为这段代码

 //Add this new Item to the array of items.
 Pantry p = new Pantry();
 p.addItemToArray(newItem);

的范围是loop

loop之前声明它并删除其他Pantry

        // Pantry p2 = new Pantry(); //remove
        p.printArray();  //change to `p`

您也不在Pantry中创建新的printArray - printArray已经是Pantry的方法

修改

此外,在printArray中,您需要使用itemArray

进行处理/打印