2011-08-29 97 views
2

我已经被赋予了一个赋值来实现ArrayList和LinkedList而不使用泛型。问题在于insertnode()方法。尽管我尝试使用扫描程序从命令行读取,但该方法无需等待即可返回。不使用泛型的ArrayList java程序

import static java.lang.System.out; 
import java.util.Scanner; 
class Arraylist 
{ 
public static final int LIST_SIZE=30; 
static Scanner input = new Scanner(System.in); 
static Object list[]; 
static int top = -1; 
static int typeoflist; 
public static void displaymenu() 
{ 
    int choice; 
    do{ 
     out.print("\n Basic operations on a linked list:"); 
     out.print("\n 1. Create list \n 2. Insert node \n 3. Delete node \n 4. Modify node \n 5. Search value \n 6. Print list\n Else. Exit \n Choice:"); 
     choice = input.nextInt(); 
     switch(choice) 
     { 
      case 1: 
       list = createlist(); 
       break; 
      case 2: 
       insertnode(); 
       break; 
      case 3: 
       //deletenode(); 
       break; 
      case 4: 
       //modifynode(); 
       break; 
      case 5: 
       //searchnode(); 
       break; 
      case 6: 
       printlist(); 
       break; 
      default: 
       return; 
     }  
    }while(true); 
} 
public static Object[] createlist() 
{ 
    int typeoflist; 
    out.println("Enter your choice of list datatype: \n 1. int \n 2. float \n 3. char \n 4. String \n 5. UserDefined \n Choice:"); 
    typeoflist = input.nextInt(); 
    switch(typeoflist) 
    { 
     case 1: 
      list = new Integer[LIST_SIZE]; 
      break; 
     case 2: 
      list = new Float[LIST_SIZE]; 
      break; 
     case 3: 
      list = new Character[LIST_SIZE]; 
      break; 
     case 4: 
      list = new String[LIST_SIZE]; 
      break; 
    } 
    return (Object[])list; 
} 
public static void insertnode() 
{ 
    Object o; 
    top++; 
    out.println("Enter the value to insert:"); 
    switch(typeoflist) 
    { 
     case 1: 
      o = (Integer)input.nextInt(); 
      list[top] = o; 
      break; 
     case 2: 
      o = (Float)input.nextFloat(); 
      list[top] = o; 
      break; 
     case 3: 
      //o = (Character)input.next(); // 
      //list[top] = o; 
      break; 
     case 4: 
      o = (String)input.next(); 
      list[top] = o; 
      break; 
    } 
} 
public static void printlist() 
{ 
    for(int i =0; i<top; i++) 
    { 
     out.println(list[i]); 
    } 
} 
public static void main(String[] args) 
{ 
    displaymenu(); 
} 
} 

回答

3

提示:typeoflistcreateList()被隐藏静态成员变量。

+0

谢谢。解决了它。 – John

+0

现在我需要尝试与LinkedList相同。随着所有的演员,它会变得复杂的我! – John