当前位置: 首页 > 知识库问答 >
问题:

用多种元素类型从文本文件中填充ArrayList

燕正卿
2023-03-14

我要填写一份来自。但是,ArrayList包含具有多个“变量”的对象(项)。

类项“变量”是字符串

public Item(String aName, String aDescription, boolean canTake) {

这是我在另一个类中创建/填充ArrayList的内容

public List<Item> loadItems() throws FileNotFoundException{
    File f = new File("Item.txt");      
      Scanner sn = new Scanner(f);
         while (sn.hasNext()){
            String itm = sn.nextLine();
            Scanner sc = new Scanner(itm);
            sc.useDelimiter("#"); 
            String aName = sc.next();
            String aDescription=sc.next();
            Boolean canTake = sc.nextBoolean();
            addItem(aName, aDescription, canTake);
                }
                return items;
            }

在第三个类中,我创建了另一个ArrayList并调用loadItems()方法,以便可以引用特定的项对象(即列表中元素0处的项对象)。

    public Actions() throws FileNotFoundException {
        loadItems();
        this.itemSlist = loadItems();

ItemList HomeList = new ItemList();
        HomeList.add(itemSlist.get(0));
        HomeList.add(itemSlist.get(1));

问题是这并没有填满数组列表

我知道对于ArrayList,你通常只会列出。添加();但每个对象都有多个变量,而不仅仅是字符串或布尔值。

我尝试将loadItems方法移动到不同的类中,并尝试使用带有txtfile的基本扫描程序,可以访问单个单词项对象,但不能访问多变量项。

共有1个答案

杨豪
2023-03-14

尝试以下方法:

public class Main{

    public static void main(String[] args) {
        
        ArrayList<Item> listItem = new List().getList();
        System.out.println("Name: " + listItem.get(0).getaName());
        System.out.println("Description: " + listItem.get(0).getaDescription());
        System.out.println("CanTake: " + listItem.get(0).isCanTake());

    }
}

班级列表:

public class List {
    
           public ArrayList<Item> getList(){
                    
                ArrayList<Item> listItems = new ArrayList<>();
                    listItems.add(new Item("Name 1", "Description 1", true));
                    listItems.add(new Item("Name 2", "Description 2", false));
                    listItems.add(new Item("Name 3", "Description 3", true));
                    listItems.add(new Item("Name 4", "Description 4", false));
                    listItems.add(new Item("Name 5", "Description 5", false));
                return listItems;
            }        
    }

类项目:

public class Item {
    
    private String Name;
    private String Description;
    private boolean CanTake;

    public Item(String Name, String Description, boolean CanTake) {
        this.Name = Name;
        this.Description = Description;
        this.CanTake = CanTake;
    }
    
    public String getaName() {
        return Name;
    }

    public void setaName(String aName) {
        this.Name = aName;
    }

    public String getaDescription() {
        return Description;
    }

    public void setaDescription(String aDescription) {
        this.Description = aDescription;
    }

    public boolean isCanTake() {
        return CanTake;
    }

    public void setCanTake(boolean canTake) {
        this.CanTake = canTake;
    }
    
}

结果:

run:
Name: Name 1
Description: Description 1
CanTake: true
BUILD SUCCESSFUL (total time: 0 seconds)

我还没有检查txt文件的导入

史戴菲

 类似资料:
  • 问题内容: 如何从文本文件填充? 问题答案: 非常模糊的问题。您是说要每行输入一个吗?如果是这样,则要使用BufferedReader之类的东西,请读取所有行,并将它们保存为String数组。创建一个新的JComboBox传入该String构造函数。

  • 代码: 我正在尝试从通过Read_file按钮读取的文本文件填充jlist 我能够正确获取文件路径和文件内容,我用print语句验证了这些文件,但我的jlist仍然是空的。在设计中,我检查了jlist的变量名,两者都与我在代码中使用的匹配。

  • 我有这个文本文件: 我的观点是: 我希望将每一行与每列相匹配(first_row-first_column,SEND_ROW-SEND_COLUMN等)我哪里出错了?我的代码如下:

  • 我使用的是带有AppCompatEditText的TextInputLayout,它为AppCompatEditText提供了一个基于xml形状的自定义背景。每当我设置一些错误,错误线从布局的开始。有没有办法给那个错误行做填充。

  • 我正在尝试用名字填充JList,这些名字最终将添加到AddressBook GUI中。我想我的逻辑是,创建JList,通过一个从文本文件中读取名称的方法填充它,然后将JList添加到我的面板中。 我的文本文件如下所示: 但它只是打印出1-10个,而不是名字。我假设这是因为我使用了而不是其他东西,有什么建议吗?

  • 问题内容: 我是Java新手,我有一个像这样的文本文件 我想用此文本文件中的数据填充“ jTable”。下面是到目前为止我的代码不起作用。当我执行程序时,表上没有任何显示。 有人可以帮帮我吗? 问题答案: 您需要将其更改为以下内容。每次读取新行时,都需要重置矢量,否则它包含第一行+第二行+以此类推。您还可以调用以避免初始行为空。并且您只需要添加行,您的注释[单元格包含很多列]的问题是由于 使用而引