试图克隆一个数组列表,但我得到一个警告



我正在编写一个程序,您可以在其中创建配料,也可以使用这些配料创建菜单。

我有一个控制器类,它反过来控制其他类。这个叫做CucharitaGUI它同时控制InventoryModuleControllerGUI和MenuModuleControllerGUI。我有一个名为InventoryManager类成分的ArrayList(连接到inventorymodulecontrolllergui)和另一个ArrayList,我想在menumodulecontrolllergui类中具有相同的成分对象,并且能够在不编辑原始ArrayList(来自InventoryManager)的情况下编辑它们。由于这个原因,我试图克隆它,但我得到一个警告。

CucharitaGUI类:

//Attributes
public UserManager userManager;

private StaffModuleControllerGUI staffModule;

public InventoryModuleControllerGUI inventoryModule;

public MenuModuleControllerGUI menuModule;

public OrderModuleControllerGUI orderModule;

//private InventoryManager inventoryManager; 

private Stage loginStage;

//@FXML Attributes:

@FXML
private PasswordField pFLogin;
@FXML
private Button btnLogIn;
@FXML
private TextField txtUserLogin;
@FXML
private Pane loginPane;

@FXML
private Pane taskManagerPane;


public CucharitaGUI() 
{
staffModule = new StaffModuleControllerGUI(this);  
inventoryModule = new InventoryModuleControllerGUI(this);
menuModule = new MenuModuleControllerGUI(this);
orderModule = new OrderModuleControllerGUI(this);
userManager = new UserManager();
//inventoryManager = new InventoryManager();
loginStage = new Stage();

}
@FXML
private void openInventoryModule(ActionEvent event) throws IOException 
{

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("inventoryModule.fxml"));
fxmlLoader.setController(inventoryModule);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root);
loginStage.setScene(scene);
loginStage.setTitle("InventoryModule");
loginStage.show(); 

inventoryModule.initializeTableView();
inventoryModule.initializeComboBox();
}
@FXML
void openMenuModule(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menuModule.fxml"));
fxmlLoader.setController(menuModule);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root);
loginStage.setScene(scene);
loginStage.setTitle("MenuModule");
loginStage.show(); 

menuModule.initializeTableView();
menuModule.initializeComboBox();
}

InventoryModuleController类:

//Attributes
private Stage inventoryModuleStage;

private ObservableList<Ingredient> observableInventoryList;

private ObservableList<String> observableUnitsList;

public InventoryManager inventoryManager;

private CucharitaGUI cucharitaGUI;


public InventoryModuleControllerGUI(CucharitaGUI cucharitaGUI){

setInventoryModuleStage(new Stage());
inventoryModulePane = new Pane();
inventoryManager = new InventoryManager();
this.cucharitaGUI = cucharitaGUI;

}

InventoryManager类:

public class InventoryManager {

public ArrayList<Ingredient> ingredients;

public InventoryManager()
{
ingredients = new ArrayList<Ingredient>();
}
public ArrayList<Ingredient> getIngredients()
{
return ingredients;
}

MenuModuleControllerGUI类:

ArrayList<Ingredient> newIngredientsToPreview;                     
public MenuManager menuManager;
private CucharitaGUI cucharitaGUI;

private boolean dishNameReadyToCreate=false;

private boolean dishIngredientsReadyToCreate=false;
public MenuModuleControllerGUI(CucharitaGUI cucharitaGUI){
setMenuModuleStage(new Stage());
menuModulePane = new Pane();
menuManager = new MenuManager();
ArrayList<Ingredient>ingredientClone=cucharitaGUI.
inventoryModule.inventoryManager.getIngredients();
newIngredientsToPreview= (ArrayList<Ingredient>)ingredientClone.clone();
this.cucharitaGUI = cucharitaGUI;       
}

我在这里得到警告:

newIngredientsToPreview= (ArrayList<Ingredient>)ingredientClone.clone();

上面写着
Type safety: Unchecked cast from Object to ArrayList<Ingredient>

有谁知道怎么修理它吗?

不要使用clone()。它坏得很厉害。

使用复制构造函数:

newIngredientsToPreview = new ArrayList<>(ingredientClone);

最新更新