抱歉,如果代码看起来很乱。
新手在这里。 我试图让用户根据他们的输入删除App类型的对象,而ArrayList只接受类型' App'的对象。但是,我无法删除它,因为我在if语句中创建了一个App类型的对象,但我不知道如何访问它。有没有什么方法可以删除和编辑由其他if语句创建的对象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);}
答案 0 :(得分:0)
如果我理解正确,您只需要在Shop类中添加另一种方法:
public void deleteApp(int index){apps.remove(index);}
现在你只支持通过对象引用删除,但底层的ArrayList也可以通过索引删除。如果您公开该接口,您的主代码将起作用...
编辑:我之前有Integer而不是int。这是一个Object,所以它会尝试删除匹配的对象。要通过索引删除,我需要使用int声明它,如果传入一个Integer,则使用Java box / unbox。
答案 1 :(得分:-1)
更改
public static void wholeSystem()
签名:
public static List<App> wholeSystem()
然后:
List<App> appList = new ArrayList<App>();
if (input == 1){
appList = wholeSystem();
}
然后你可以在if(input == 1)block〜
之后删除anyApp如果您尝试使用列表中的索引位置删除应用程序,请执行以下操作:
public void deleteApp(int index){
App removedApp = apps.remove(index);
}
请注意,上面的remove方法也会返回Object,这样你就可以用它做任何你想做的事了〜