开发者

ArrayList java program without using generics

开发者 https://www.devze.com 2023-04-01 12:45 出处:网络
I had been given an assignment to implement ArrayList and LinkedList without using generics. The problem is with the insertnode() method. Though I try to read from commandline using a scanner, the met

I had been given an assignment to implement ArrayList and LinkedList without using generics. The problem is with the insertnode() method. Though I try to read from commandline using a scanner, the method returns without waiting.

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{开发者_StackOverflow社区
        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();
}
}


Hint: typeoflist in createList() is hiding the static member variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消