2016-02-02 82 views
-1

這裏是我的輸出:ArrayList的<String>刪除查詢

-----Query----- 
[1]Update 
[2]Delete 
[3]Search 
[4]Show 
Choose Query:1 
Enter Your Student ID:1 
Enter Your First Name: Respo 
Enter Your Middle Name: Topher 
Enter Your Last Name: Raspo 
Do you want to back to Query?(Yes/No) 
Yes 
-----Query----- 
[1]Update 
[2]Delete 
[3]Search 
[4]Show 
Choose Query: 4 
12 
Christopher 
Reposo 
Porras 
1 
Respo 
Topher 
Raspo 

正如你可以在圖片中看到,我試圖讓沒有數據庫一個簡單的小系統,但使用ArrayList中包含這些數據我現在的問題是在刪除查詢。現在在刪除查詢中,我告訴用戶鍵入1的學生號碼,然後刪除它的信息和它的包含名字,中間名,姓氏的信息。但是我沒有太多邏輯在ArrayList中做這樣的事情。順便說一下,在這種情況下可能只使用一個ArrayList,或者我需要製作多個數組列表來解決我的問題。

public static void main(String[] args) { 
    //initialize Scanner for input process 
    Scanner scan = new Scanner(System.in); 

    //initialize needs variable 
    List<String> list = new ArrayList<String>(); 
    int choose,chooseQuery; 
    String chooseYesOrNo = " "; 
    String chooseYesOrNo2 = " ";  

    do { 
     //Startup Program 
     System.out.println("=====-----LibrarySystem-----====="); 
     System.out.println("[1]Student Information"); 
     System.out.println("[2]Book Information"); 
     System.out.print("Choose Table:"); 
     choose = scan.nextInt(); 

     do {    
      if(choose == 1) { 
       System.out.println("-----Query-----"); 
       System.out.println("[1]Update"); 
       System.out.println("[2]Delete"); 
       System.out.println("[3]Search"); 
       System.out.println("[4]Show"); 
       //reserved 
       //reserved 
       System.out.print("Choose Query:"); 
       chooseQuery = scan.nextInt(); 

       if(chooseQuery == 1) {      
        System.out.print("Enter Your Student ID:"); 
        String id = scan.next(); 
        list.add(id); 
        System.out.print("Enter Your First Name:"); 
        String name = scan.next(); 
        list.add(name); 
        System.out.print("Enter Your Middle Name:"); 
        String middle_name = scan.next(); 
        list.add(middle_name); 
        System.out.print("Enter Your Last Name:"); 
        String last_name = scan.next(); 
        list.add(last_name);   

        System.out.println("Do you want to back to Query?(Yes/No)"); 
        chooseYesOrNo = scan.next();        
       } else if (chooseQuery == 2) { //Delete Query 
        System.out.print("Enter Student ID:"); 
        String find_id = scan.next();        
       } else if(chooseQuery == 3) { //Search Query 

       } else if(chooseQuery == 4) { //Show Query 
        for (String s : list) { 
         System.out.println(s); 
        } 
       } 
      } 
     } while(chooseYesOrNo.equals("Yes")); 

     System.out.println("Do you want to get back at tables?(Yes/No)"); 
     chooseYesOrNo2 = scan.next();         
    } while(chooseYesOrNo2.equals("Yes")); 

    System.out.println("-----=====Program Terminated=====-----"); 
} 
+1

請勿張貼圖片,文本後。 –

+0

看到您現在的代碼會很有幫助。 – kunruh

+0

我的錯我會馬上發帖 – Cypher

回答

3
  1. 創建一個包含所有你需要的字段(學號,姓名等)

    class Student { 
        int studentId; 
        String firstname; 
        String middlename; 
        String lastname; 
    } 
    
  2. 有一個數組列表Student對象

    java.util.List<Student> list = new java.util.ArrayList<Student>(); 
    
  3. Student對象

    選擇刪除操作時,遍歷列表以查找對象並將其刪除。有關如何遍歷arraylist的nice blog。我最喜歡的方法如下:

    for (Student std:list) { 
        if (std.studentId == targetId) { 
        list.remove(std); 
        break; //since you've removed target, you can exit the loop 
        } 
    } 
    
+0

瑞安爵士我在這段代碼有問題。 (學生std:列表){ if(std.studentId == targetId){ list.remove(std); 休息; //因爲你已經刪除的目標,你可以退出循環 }} 我得到的錯誤,它說對象不能轉換爲列出 我知道它太多spoonfeeding在我對不起。 – Cypher