2015-06-22 53 views
0

循環我有這樣的代碼,涉及任務做的ArrayListnames ,其包括像添加元素在ArrayList,顯示ArrayList的元素,在一ArrayList刪除的元素,在ArrayList編輯元件功能。與ArrayList的

我的問題是當我在ArrayList中添加一個新名稱,當代碼循環回來並且我想查看ArrayList中的元素時,它變爲空白。

import java.util.ArrayList; 
import java.util.Scanner; 

public class mainRunner { 
    public static void main(String[] args) { 
     String con; 
     do { 
      menuCaller(); 
      System.out.println("Do you want to continue[Y/N]"); 
      Scanner confirm = new Scanner(System.in); 
      con = confirm.nextLine(); 
     } while (con.equalsIgnoreCase("y")); 
    } 

    public static void menuCaller() { 
     int choice; 
     ArrayList names = new ArrayList(); 
     int firstIndex =0; 
     Scanner scan = new Scanner(System.in); 
     Scanner scan2 = new Scanner(System.in); 
     Scanner scaned = new Scanner(System.in); 
     System.out.println("Menu" 
         +"\n[1] Add Name" 
         + "\n[2] Display All Record" 
         + "\n[3] Delete a Record" 
         + "\n[4] Edit a R1ecord " 
         + "\n[5] Exit"); 

     choice = scaned.nextInt(); 
     if (choice == 1) { 
      System.out.println("***ADDING NAMES***"); 
      System.out.println("Enter the name of the Student"); 
      String addName = scan.next(); 
      names.add(addName); 
      System.out.println("Record Added !!!");  
     } 
     else if (choice == 2) { 
      System.out.println("***Database Content**"); 
      System.out.println("------------------------"); 
      System.out.println("Record # | Name "); 

      for (int index = firstIndex; index < names.size(); ++index) { 
       System.out.println("--------------------"); 
       System.out.println(" " + index + " | " +names.get(index)); 
      } 
     } 
     else if (choice == 3) { 
      System.out.println("***Delete A Record***"); 
      System.out.print("Enter a number to be deleted: "); 
      int numDelete = scan.nextInt(); 
      names.remove(numDelete); 
      System.out.println("Record Deleted"); 
     } 
     else if (choice == 4) { 
      System.out.println("***Edit Record***"); 
      System.out.println("***Database Content**"); 
      System.out.println("------------------------"); 
      System.out.println("Record # | Name "); 

      for (int index = firstIndex; index < names.size(); ++index) { 
       System.out.println("--------------------"); 
       System.out.println(" " + index + " | " +names.get(index)); 
      } 

      System.out.println("Enter the Name to be Edited"); 
      String nameEdited = scan.next(); 
      if (names.indexOf(nameEdited) >= 0) { 
       System.out.print("Change to: "); 
       String nameChange = scan2.next(); 
       names.set(names.indexOf(nameEdited), nameChange); 
      } 
      else { 
       System.out.println("Record Not Existing"); 
      } 
     } 
     else if (choice == 5) { 
      System.out.println("Thank you for using the Program"); 
      System.exit(choice); 
     } 
     else { 
      System.out.println("Wrong Choice"); 
     } 
    } 
} 
+2

在'menuCaller()'你始終創建的'names'一個新的(空)名單。 – Marvin

+0

爲什麼每次你想要從System.in讀取數據時都要創建新的掃描儀?創建一個並重用它。 – Pshemo

回答

4

每當您撥打menuCaller時,都會創建一個新列表。

也許你應該在main方法創建並傳遞到參考menuCaller

public static void main(String[] args) { 
    String con; 
    List<String> names = new ArrayList<>(); 
    do{ 
     menuCaller(names); 
     System.out.println("Do you want to continue[Y/N]"); 
     Scanner confirm = new Scanner(System.in); 
     con = confirm.nextLine();  
    }while(con.equalsIgnoreCase("y")); 
} 

public static void menuCaller(List<String> names) { 
    ... 
    // (No declaration of names here - it's a parameter now...) 
    ... 
} 
+0

愛德華,請注意,喬恩添加了一個泛型類型的名單「名稱」。請閱讀這個問題,爲什麼你應該避免原始類型:[什麼是原始類型,爲什麼我們不應該使用它?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type - 和 - 爲什麼 - 不應該,我們使用-IT) – Tom