简体   繁体   English

将项目添加到ArrayList

[英]Adding item to an ArrayList

I have an array list of Fleets (Each fleet will hold its own list of Trucks). 我有一个舰队列表(每个舰队将拥有自己的卡车列表)。

I have a fleet class with the constructor 我有一个与建设者的舰队班

public Fleet(String businessName){
    this.businessName = businessName;
    this.fleetList = new ArrayList<Truck>();
}

So: 所以:

In my TUI, I have a helper method called createFleet. 在我的TUI中,我有一个名为createFleet的帮助程序方法。 When the user presses 1 on the menu, it asks for a name of his business, and then makes a fleet named that. 当用户在菜单上按1时,它会询问他的公司名称,然后制作一个名为该公司的车队。 That method is: 该方法是:

public static void createFleet(){
    System.out.println("");
    System.out.println("Please enter the name of the fleet.");
    inputText = scan.nextLine();

    fleetCollection.add(new Fleet(inputText));
    printFleets();
    System.out.println("");
    System.out.println("--- Fleet: " + inputText + " added ---");
    System.out.println("");
}

And my problem is that when I add one Fleet, and print the results I get: 我的问题是,当我添加一个Fleet并打印结果时,我得到:

Fleet 0: Fleet Number One

But when I add Fleet Number One, then press 1 on the menu again to add ANOTHER fleet (named Fleet Number Two) and print the fleet list, the results are: 但是,当我添加第一舰队时,然后再次按菜单上的1以添加另一个舰队(名为第二舰队)并打印舰队列表,结果是:

Fleet 0: Fleet Number Two
Fleet 1: Fleet Number Two

It seems to be confusing the two...and this further breaks the program when I try to add trucks to the fleet, because It cannot pick the "right" fleet. 这似乎使两者混淆了……当我尝试向车队中添加卡车时,这进一步破坏了程序,因为它无法选择“正确的”车队。

Please let me know if you need any other of my code. 如果您需要我的其他代码,请告诉我。 I just need this to correctly add and print the fleets in the fleet list: 我只需要这个就可以正确添加并打印舰队列表中的舰队:

private static ArrayList<Fleet> fleetCollection;

Thank you :) for all the help! 谢谢:)提供的所有帮助!

您可能已在Fleet类中将businessName声明为static ,如果是,则将其删除

You need to be clear about use of static 您需要清楚使用static

static variable 静态变量

  • It is a variable which belongs to the class and not to object(instance) 它是一个属于类而不属于对象(实例)的变量
  • Static variables are initialized only once , at the start of the execution . 静态变量在执行开始时仅初始化一次。 These variables will be initialized first, before the initialization of any instance variables 在初始化任何实例变量之前,将首先初始化这些变量
  • A single copy to be shared by all instances of the class 该类的所有实例共享一个副本
  • A static variable can be accessed directly by the class name and doesn't need any object 可以通过类名称直接访问静态变量,并且不需要任何对象
  • Syntax : <class-name>.<variable-name> 语法: <class-name>.<variable-name>

static method 静态方法

  • It is a method which belongs to the class and not to the object(instance) 它是属于类而不是对象(实例)的方法
  • A static method can access only static data. 静态方法只能访问静态数据。 It can not access non-static data (instance variables) 它无法访问非静态数据(实例变量)
  • A static method can call only other static methods and can not call a non-static method from it. 静态方法只能调用其他静态方法,而不能从中调用非静态方法。
  • A static method can be accessed directly by the class name and doesn't need any object 静态方法可以通过类名直接访问,不需要任何对象
  • Syntax : <class-name>.<method-name> 语法: <class-name>.<method-name>
  • A static method cannot refer to " this " or " super " keywords in anyway 静态方法无论如何都不能引用“ this ”或“ super ”关键字

src : http://www.javatutorialhub.com/java-static-variable-methods.html src: http//www.javatutorialhub.com/java-static-variable-methods.html

have you overridden the equals method in Fleet ? 您是否在Fleet中重写了equals方法? if so and it's not correct, it could be the cause of your weird result 如果是这样,那是不正确的,那可能是导致您结果怪异的原因

I made small modifications to your program to make it simple. 我对您的程序做了一些小的修改以使其变得简单。

class Fleet{
String businessName;
public Fleet(String businessName)
{ this.businessName = businessName;}
public String getBusinessName()
{
    return businessName;
}
}

public class T {
private static ArrayList<Fleet> fleetCollection = new ArrayList<Fleet>();

    public static void main(String[] args) 
    {
        createFleet("A");
         printFleets();
         createFleet("B");
        printFleets();
    }
     public static void createFleet(String name){
    System.out.println("");
    fleetCollection.add(new Fleet(name));
}
public static void printFleets(){
    Iterator i = fleetCollection.iterator();
    Fleet f;
    while(i.hasNext())
    {
        f = (Fleet)i.next();
        System.out.println(f.getBusinessName());
    }
}

}

It prints as expected. 它按预期打印。

A

A
B

Check your access modifiers on "businessName" field. 检查“ businessName”字段上的访问修饰符。 It shouldn't be static. 它不应该是静态的。 Also check printFleets() method. 还要检查printFleets()方法。

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

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