[英]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 静态变量
<class-name>.<variable-name>
<class-name>.<variable-name>
static method 静态方法
<class-name>.<method-name>
<class-name>.<method-name>
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.