【问题标题】:Trying to make a clone of an ArrayList but I get a warning [duplicate]试图克隆 ArrayList 但我收到警告 [重复]
【发布时间】:2021-11-18 19:26:15
【问题描述】:

我正在制作一个程序,您可以在其中创建成分并使用这些成分创建菜单。

我有一个控制器类,它反过来控制其他类。这个叫做 CucharitaGUI,它控制 InventoryModuleControllerGUI 和 MenuModuleControllerGUI。我在名为 InventoryManager(连接到 InventoryModuleControllerGUI)的类中有一个成分的 ArrayList 和另一个 ArrayList,我希望在 MenuModuleControllerGUI 类中有相同的成分对象,并且能够在不编辑原始 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&lt;Ingredient&gt;

有人知道怎么解决吗?

【问题讨论】:

  • 您可以阅读this 来完成您的任务。

标签: java arraylist clone


【解决方案1】:

不要使用clone()。是horribly broken

使用复制构造函数:

newIngredientsToPreview = new ArrayList<>(ingredientClone);

【讨论】:

  • 对不起,我忘了说我需要编辑 newIngredientsToPreview 数组列表而不编辑原始成分数组列表
  • @JokerFever 这会创建一个新列表。更改它不会更改原始列表。
猜你喜欢
  • 2013-12-07
  • 2011-11-15
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多