不使用泛型的ArrayList java程序

发布于 2024-12-01 18:06:42 字数 2585 浏览 0 评论 0原文

我被分配了在不使用泛型的情况下实现 ArrayList 和 LinkedList 的任务。问题出在 insertnode() 方法上。尽管我尝试使用扫描仪从命令行读取,但该方法无需等待即可返回。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-12-08 18:06:42

提示:createList() 中的typeoflist 隐藏了静态成员变量。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文