gpt4 book ai didi

java - 不使用泛型的 ArrayList java 程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:03 25 4
gpt4 key购买 nike

我接到了一项任务,要在不使用泛型的情况下实现 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();
}
}

最佳答案

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

关于java - 不使用泛型的 ArrayList java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7226141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com