卢同学的Java笔记_1:顺序表--数据结构

本文是作者寒水作为大二电子商务学生学习Java的笔记,主要讲解了顺序表这一数据结构。通过《Java常用算法手册》学习,分享所敲代码,期待读者和大佬的指正,以助于快速掌握Java编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hey,我是寒水,一名大二学生,电子商务专业在读,正在学习Java中。我试图将在Java学习中遇到的一些困惑和最终的解答发在这个账号上,希望以此来激励我不要放弃学习!

import java.util.*;

class Monster{		//定义一个怪物类
	String name;
	int hp;
	int mm;
	String skills;
	String drop; 
}					


class monsterHome{
	static final int MAXLEN=100; //最多存在100种怪物
	Monster[]ListData=new Monster[MAXLEN];
	int ListLen;			//定义怪物列表元素
	
	
	
	void begin(monsterHome world) {		
		world.ListLen=0;					
	}						//  初始化世界怪物列表为空表
	
	
	
	int monsterNumber(monsterHome world) {
		return(world.ListLen);
	}					//返回目前列表中的怪物种类
	

	int monsterBorn(monsterHome world,int n,Monster monster) {  		//插入怪物数据
		if(world.ListLen>=MAXLEN) {
			System.out.println("Sorry, the list was full:)");
			return 0;			//插入失败,返回0值
		}						//如果特定编号超出了最大值,则插入不成功
		if(n<0||n>MAXLEN) {
			System.out.println("the number of the monster was wrong!");
			return 0;
		}						//插入怪物的编号不合法,插入失败
		
		
		
		for(int i=world.ListLen;i>=n;i--) {
			world.ListData[i+1]=world.ListData[i];
		}
		
		world.ListData[n]=monster;
		world.ListLen++;
		return 1;
	}     						//插入怪物数据

	

		int monsterAdd(monsterHome world,Monster monster) {			//在列表尾部增加数据
			if(world.ListLen>=MAXLEN) {
				System.out.println("Sorry,U can not do this way. ");
				return 0;
			}					//超出范围,插入失败
			world.ListData[++world.ListLen]=monster;
			return 1;
		}						//在尾部增加数据

		
		
		int monsterDelete(monsterHome world,int n) {				//删除列表元素	
			if(n<1||n>world.ListLen) {			
				System.out.println("sorry,you can not do it this way. ");
				return 0;
			}									//超过范围,删除失败,返回0值
			for(int i=n;i<world.ListLen;i++) {			
				world.ListData[i]=world.ListData[i+1];
			}
			world.ListLen--;
			return 1;							//删除成功,返回0值
		}
		
		
		
			Monster monsterFindByNumber(monsterHome world,int n) {				//通过序号查找怪物
				if(n<1||n>world.ListLen) {
					System.out.println("Sorry, you cannot do it this way. ");
					return null;				//序号部队,查找失败
				}
				return ListData[n]; 			//返回怪物属性
			}
			
			int monsterFindByName(monsterHome world,String name) {				//通过怪物名字查找怪物序号
				int i;
				for(i=0;i<=world.ListLen;i++) {
					if(world.ListData[i].name.compareTo(name)==0){
						return i;			//查找成功,返回怪物序号
							}
						}
					return 0;
			}
	

			int monsterAll(monsterHome world) {					//输出怪物列表所有怪物
				for(int i=1;i<=world.ListLen;i++) {
					System.out.printf("(%s,%d,%d,%s,%s)\n",world.ListData[i].name,world.ListData[i].hp,world.ListData[i].mm,world.ListData[i].skills,world.ListData[i].drop);
				}
				return 0;
			}
}
public class theOrderSheet {												//顺序表应用
	public static void main(String[] args) {
		monsterHome world=new monsterHome();
		Monster monster;
		String name;
		int a;
		
		world.begin(world);										//初始化表格
		System.out.println(".");
		System.out.println("..");
		System.out.println("...");
		System.out.println("世界初始化完成...");
		
		Scanner input=new Scanner(System.in);
		
		do {																//输入怪物
			monster=new Monster();
			System.out.println("请输入怪物名称,输入“退出输入”以退出输入模式");
			monster.name=input.next();
			if(monster.name.equals("退出输入")) {
				break;
			}
			System.out.println("请输入怪物血量");
			monster.hp=input.nextInt();
			System.out.println("请输入怪物魔法值");
			monster.mm=input.nextInt();
			System.out.println("请输入怪物技能");
			monster.skills=input.next();
			System.out.println("请输入怪物掉落");
			monster.drop=input.next();
		
			if(world.monsterAdd(world, monster)==0) {
				break;
			}
		}while(true);
		System.out.println("怪物列表如下:");
		world.monsterAll(world);										//输出怪物列表
	
		System.out.print("请输入想查看怪物的编号:");
		a=input.nextInt();
		monster=world.monsterFindByNumber(world, a);
		if(monster!=null) {
			System.out.printf("第%d个结点的怪物为%s,血量:%d,魔法值:%d,技能:%s,掉落物品:%s\n",a,monster.name,monster.hp,monster.mm,monster.skills,monster.drop);
		}
	
	}
}

这是我根据《Java常用算法手册》学习敲出的代码,如有错误还望各位大佬不吝指正!
真的万分感谢,大佬们的指导可以帮助我更快的菜鸟上路呀~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值