2

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{
        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();
}
}

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Now I need to try the same with LinkedList. With all that Casting it's going to get complicated for me!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.