2014-12-13 3 views
0

코드가 복잡 해지면 죄송합니다.다른 if 문으로 ArrayList에서 if 문으로 작성된 객체를 제거하는 방법이 있습니까?

여기 새내기. 사용자가 입력에 따라 App 유형의 객체를 제거하도록하고 ArrayList는 'App'유형의 객체 만 허용합니다. 그러나 if 문에 App 유형의 객체를 만들었 기 때문에 제거 할 수 없으며 액세스하는 방법을 모르겠습니다. 다른 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를 만든 가게 클래스입니다. 당신은 단지 객체 참조에 의해 삭제 지원 지금

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

을하지만, 기본 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

... 작동하지 않는 것은 무엇입니까? 오류 메시지, 스택 추적 또는 동작 설명을 참조하십시오. 정수 인수를 사용하여 deleteApp를 호출하고 있지만 App을 전달해야하는 선언은 없습니다. – RobP

+0

int가 앱과 호환되지 않으며 변환 할 수 없다고합니다. 그리고 사용자가 스캐너에 숫자를 입력하고 주어진 숫자를 기반으로 arraylist에서 객체를 삭제할 수 있기를 원하기 때문에 정수로 호출하려고합니다. – user3479469

+0

아래 내 대답을 참조하십시오. Arraylist가 지원하기 때문에 ArrayList의 색인을 기반으로하는 앱을 삭제할 수있는 코드는 더 이상 필요하지 않습니다. – RobP

답변

0

, 당신은 당신의 쇼핑 클래스에 다른 방법을 추가 할 필요가 또한 인덱스별로 삭제할 수 있습니다. 해당 인터페이스를 노출하면 기본 코드가 작동합니다 ...

편집 : 이전에는 정수 대신 정수가있었습니다. 그것은 Object이기 때문에 일치하는 객체를 제거하려고합니다. 인덱스로 제거하려면 Integer를 전달하면 이것을 선언하고 Java box/unbox를 전달해야합니다.

+0

코드를 실행할 수 있지만 개체를 ​​제거하려고 할 때 ArrayList에 여전히있었습니다. 또한 "Java.util.Collection.remove에 대한 의심스러운 호출 : 주어진 개체에 Integer 인스턴스가 포함될 수 없습니다 (예상되는 앱)". – user3479469

+0

@ user3479469 편집을 참조하십시오. 내 선언에서 Integer를 int로 변경하면 여기에 차이가 있습니다. – RobP

+0

정말 고마워요! 완벽하게 작동하는지 확인할 수 있습니다. 정말 감사합니다! :) – user3479469

-1

변경

공공 정적 무효의 wholeSystem()이에

서명 : 다음

public static List<App> wholeSystem() 

:

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

당신은 remov 수 있습니다 전자 anyApp 후 경우 (입력 == 1) 블록 ~

당신이 다음을 수행 목록 내부의 인덱스 위치를 사용하여 응용 프로그램을 삭제하려는 경우 :

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

를 적어 둡니다은 또한 위의 제거 방법 Object를 반환하면 원하는 모든 작업을 수행 할 수 있습니다 ~

관련 문제