简体   繁体   English

声明一个包含 Java 数组的 ArrayList

[英]Declaring an ArrayList that has an array for Java

Basically, i created a new class that contains integers and floats and made it into an array so that my array can have more than one data type:基本上,我创建了一个包含整数和浮点数的新类,并将其放入一个数组中,以便我的数组可以有多个数据类型:

public class LibRecord {
    // values for each libary user
    public int number;
    public int issued;
    public int maxLoans;
    public float fines;
    public String name;       
}

private LibRecord[] item = new LibRecord[maxUsers];

I need to make this into an arraylist so that i can delete,add and insert values and everytime i try to create an ArrayList with the name 'item', the compiler tells me that this name has already been used.我需要将它变成一个数组列表,以便我可以删除、添加和插入值,并且每次我尝试创建一个名为“item”的 ArrayList 时,编译器都会告诉我这个名称已被使用。 What do i do?我该怎么办? And how do i get this program to work so that i can call the methods in ArrayList?我如何让这个程序工作,以便我可以调用 ArrayList 中的方法?

As the compiler is clearly telling you, you can't make two fields with the same name.正如编译器明确告诉您的那样,您不能创建两个具有相同名称的字段。

You need to use different names.您需要使用不同的名称。 (or get rid of the first field) (或去掉第一个字段)

Just replace your:只需更换您的:

private LibRecord[] item = new LibRecord[maxUsers];

with:和:

private List<LibRecord> users = new ArrayList<LibRecord>(maxUsers);

Make sure that you don't have two variables named item in the same scope.确保在同一范围内没有两个名为item变量。

Or else rename item in private LibRecord[] item = new LibRecord[maxUsers];或者重命名itemprivate LibRecord[] item = new LibRecord[maxUsers]; to itemsitems

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

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