2015-06-22 26 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