【问题标题】:Is there any way to remove an object that was created by an if statement from an ArrayList with another if statement?有没有办法用另一个 if 语句从 ArrayList 中删除由 if 语句创建的对象?
【发布时间】:2014-12-13 19:42:26
【问题描述】:

抱歉,如果代码看起来很乱。

这里是新手。 我试图让用户根据他们的输入删除 App 类型的对象,而 ArrayList 只接受“App”类型的对象。但是,我无法删除它,因为我在 if 语句中创建了一个 App 类型的对象并且我不知道如何访问它。有什么方法可以删除和编辑由不同 if 语句和 else if 创建的对象?

public static void main(String[] args) throws IOException {
    // TODO code application logic here



Scanner myScan = new Scanner(System.in);



System.out.println("Enter your option from the list below");
System.out.println("1.App Store");
System.out.println("2.Customer stuff");
int input = myScan.nextInt();

if (input == 1){
    wholeSystem();
}


}





public static void wholeSystem()

{
    boolean choice = true;
    int input2 = 0;
    Shop appStore = new Shop(new ArrayList<App>());  
    while (choice == true)

            {



    Scanner myScan2 = new Scanner(System.in);
    System.out.println("Enter another option from the list below");
    System.out.println("1. Add new App details");
    System.out.println("2. Delete App details");
    System.out.println("3. Edit App details");
    System.out.println("4. List App details");
    System.out.println("5. Search App details");
    System.out.println("6. Quit");
    input2 = myScan2.nextInt(); 

    if (input2 == 6)
    {
        System.out.println("You have exited.");
         break;
    }

    else if (input2 == 1)
    {
        System.out.println("You have selected feature "+input2+".");
        Scanner myScan3 = new Scanner(System.in);
        App myObj = new App();
        System.out.println("Please enter the app name:");
        myObj.setAppName(myScan3.nextLine());
        System.out.println("Please enter the developer name:");
        myObj.setDeveloperName(myScan3.nextLine());
        System.out.println("Please enter the app's function:");
        myObj.setAppFunction(myScan3.nextLine());
        System.out.println("Please enter the app's type");
        myObj.setType(myScan3.nextLine());
        System.out.println("Please enter the app's cost:");
        myObj.setCost(myScan3.nextInt());
        System.out.println("Please enter the app's popularity:");
        myObj.setPopularity(myScan3.nextInt());
        System.out.println(myObj.getAppName()+myObj.getDeveloperName());
        appStore.addApp(myObj);




File AppStore = new File("AppStore");        
try{

BufferedWriter out;

out = new BufferedWriter ( new FileWriter(AppStore,true));


out.write(myObj.getAppName());
out.newLine();
out.write(myObj.getDeveloperName());
out.newLine();
out.write(myObj.getAppFunction());
out.newLine();
out.write(myObj.getType());
out.newLine();
out.write (String.valueOf(myObj.getCost()) );
out.newLine();
out.write(String.valueOf(myObj.getPopularity()) );
out.newLine();
out.write("========================================================");
out.newLine();
//Close the output stream
out.close();
}
catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
        }    
    }

    else if (input2 == 2)
    {
        int choiceInput = 0;
        System.out.println("You have selected feature "+input2+". Which app do you want deleted?");
        // here's where I tried to remove an object 
        appStore.printApps();
        choiceInput = myScan2.nextInt();
        appStore.deleteApp(choiceInput); with user input

这是我创建 ArrayList 的商店类。 公共类商店{

private ArrayList<App> apps = new ArrayList<App>();



public Shop(ArrayList apps)
{
    this.apps = apps;
}

public ArrayList<App> getApps(){return apps;}

public void setApps(ArrayList<App>apps){this.apps = apps;}

public void addApp(App app){apps.add(app);}

public void deleteApp(App app){apps.remove(app);}

【问题讨论】:

  • ...什么不起作用?请提供错误消息、堆栈跟踪或行为描述。请注意,您使用整数参数调用 deleteApp,但您的声明应该传递一个 App。
  • 表示int与app不兼容,无法转换。是的,我试图用一个整数来调用它,因为我希望用户能够在扫描仪中输入一个数字,并根据给定的数字从数组列表中删除一个对象。
  • 在下面查看我的答案,不需要更多代码就可以根据 ArrayList 中的索引删除应用程序,因为 Arraylist 支持。

标签: java arraylist


【解决方案1】:

如果我理解正确,您只需在 Shop 类中添加另一个方法:

public void deleteApp(int index){apps.remove(index);}

目前只支持按对象引用删除,但底层的ArrayList也可以按索引删除。如果你公开那个接口,你的主代码就可以工作了……

编辑:我之前有整数而不是整数。那是一个对象,因此它会尝试删除匹配的对象。要按索引删除,我需要用 int 声明它,如果传入 Integer,则让 Java 装箱/拆箱。

【讨论】:

  • 虽然它允许我运行代码,但当我尝试删除一个对象时,它仍然存在于 ArrayList 中。此外,它显示“对 Java.util.Collection.remove 的可疑调用:给定对象不能包含 Integer (expected App)"的实例。
  • @user3479469 请参阅编辑。在我的声明中将 Integer 更改为 int 会有所不同。
  • 非常感谢!我可以确认它工作正常!真的很感激! :)
【解决方案2】:

改变

public static void wholeSystem()

签名:

public static List<App> wholeSystem()

然后:

List<App> appList = new ArrayList<App>();   
if (input == 1){
   appList = wholeSystem();
}

那么你可以在 if (input == 1) 块之后删除 anyApp ~

如果您尝试使用列表中的索引位置删除应用,请执行以下操作:

public void deleteApp(int index){
    App removedApp = apps.remove(index);
}

请注意,上面的 remove 方法也会返回 Object,所以你可以用它做任何你想做的事~

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2020-11-17
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    相关资源
    最近更新 更多