数组队列学习总结

 

数组队列

 

1.为什么要使用数组队列?

 

数组相当于是一个容器,可以存放多个相同类型的数据。

 

优点:有序性,清晰,可以快速地查找数据;具有连续的存储空间

 

缺点:在定义的时候数组长度已经固定,不可改变。例如在画板存储图形的时候,如果数组长度太小,会造成画了一定的图形之后,没有存储空间。如果数组长度太大,会造成存储空间的浪费。

 

2.数组队列的实现

 

数据类型 [ ] 数组名 = new 数据类型 [数组长度]

 

对象名中存储什么?对象在内存中的首地址。

 

数组名中存储什么?数组对象在内存中的首地址。

 

数组队列是通过改变数组的首地址来改变数组的长度和内容

 

3.数组队列实现步骤

 

1.定义一个接口,在接口中定义需要实现的抽象方法

 

2.定义一个类来实现接口,重写接口中的方法

 

3.实现方法

 

4.注意事项

 

1.首先要注意的是在实现抽象方法,比如移除、添加等方法的时候,注意数组队列长度的变化

 

2.一定要让原数组指向暂时存储数据的数组

 

 

//主函数
public class Manager {

	public static void main(String[] args) {
		System.out.println("开始测试");

		MyList ml1 = new MyListImpl();

		MyList ml2 = new MyListImpl();
		for (int i = 0; i < 3; i++) {
			ml2.add(new Student("m2学生" + i, i));
			System.out.println("m2学生" + i + "学号" + i);
		}
		//
		System.out.println("这是ml1");
		for (int i = 0; i < 2; i++) {
			ml1.add(new Student("学生" + i, i));
			System.out.println("学生" + i + "学号" + i);
		}

		Student stu = new Student("学生1", 1);

		// 添加新元素
		ml1.add(stu);
		System.out.println("给ml1加上stu");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 在指定位置添加一个新元素
		ml1.add(1, stu);
		System.out.println("给ml1在指定位置1添加一个新元素加上stu");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 将数组队列ml2中的数据添加到当前数组队列的末尾
		ml1.add(ml2);
		System.out.println("将数组队列ml2中的数据添加到当前数组队列的末尾");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 在指定位置2将参数数组队列ml2中的元素添加到该位置
		ml1.add(2, ml2);
		System.out.println("在指定位置2将参数数组队列ml2中的元素添加到该位置");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 移除指定索引位置的元素
		ml1.remove(2);
		System.out.println("移除指定索引位置2的元素");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 移除指定的元素对象
		ml1.remove(stu);
		System.out.println("移除指定的元素对象");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 移除mal数组队列中的元素
		ml1.remove(ml2);
		System.out.println("移除ml2数组队列中的元素");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 修改指定索引位置1的元素,用stu来代替
		ml1.set(1, stu);
		System.out.println("修改指定索引位置1的元素,用stu来代替");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 获取指定索引位置的元素
		ml1.get(0);
		System.out.println("获取指定索引位置的元素");
		for (int i = 0; i < ml1.size(); i++) {
			Student student = ml1.get(i);
			System.out.println("学生是" + student.getName());
		}
		// 返回数组队列中首次出现的指定元素的索引
		System.out.println("返回数组队列中首次出现的指定元素的索引" + ml1.indexOf(stu));
		// 判断数组队列中是否有元素
		System.out.println("判断数组队列中是否有元素" + ml1.isEmpty());
		// 返回此列表中最后一次出现的指定元素的索引
		System.out.println("返回此列表中最后一次出现的指定元素的索引" + ml1.lastIndexOf(stu));
		// 清除数组队列中所有的元素
		ml1.clear();
		System.out.println("清除数组队列中所有的元素");
	}
}

 

//接口
public interface MyList {
	/**
	 * 添加新元素的方法
	 * @param stu是新元素
	 */
	public void add(Student stu);
	
	/**
	 * 在指定位置添加一个新元素
	 * @param index指定的索引位置
	 * @param stu要添加的新元素
	 * @return 返回true表示添加成功,返回false表示添加失败
	 */
	public boolean add(int index ,Student stu);
	
	/**
	 * 将数组队列mal中的数据添加到当前数组队列的末尾
	 * @param mal要被添加的数组队列
	 */
	public void add(MyList mal);
	
	/**
	 * 在指定位置将参数数组队列中的元素添加到该位置
	 * @param index指定的索引位置
	 * @param mal要被添加的数组队列
	 * @return 返回true表示添加成功,返回false表示添加失败
	 */
	public boolean add(int index,MyList mal);
	
	/**
	 * 移除指定索引位置的元素
	 * @param index指定的索引位置
	 * @return 返回被移除的对象,如果返回Null则表示index的索引不存在
	 */
	public Student remove(int index);
	
	/**
	 * 移除指定的元素对象
	 * @param stu要被移除的元素
	 * @return 返回true表示移除成功,返回false表示移除失败
	 */
	public boolean remove(Student stu);

	/**
	 * 移除mal数组队列中的元素
	 * @param mal要被移除的数组队列
	 * @return 返回true表示移除成功,返回false表示移除失败
	 */
	public boolean remove(MyList mal);
	
	/**
	 * 修改指定索引位置的元素,用stu来代替
	 * @param index指定的索引位置
	 * @param stu要替换的新元素
	 * @return 返回被替换的对象,如果返回Null则表示index的索引不存在
	 */
	public Student set(int index,Student stu);
	
	/**
	 * 获取指定索引位置的元素
	 * @param index指定的索引位置
	 * @return 返回索引位置的对象,如果返回Null则表示index的索引不存在
	 */
	public Student get(int index);
	
	/**
	 * 返回数组队列中存储的元素总数
	 * @return 返回元素总数
	 */
	public int size();
	
	/**
	 * 清除数组队列中所有的元素
	 */
	public void clear();
	
	/**
	 * 返回数组队列中首次出现的指定元素的索引,或如果此数组队列不包含元素,则返回 -1。
	 * @param stu要查找的元素
	 * @return  返回元素的索引,如果返回-1表示元素不存在
	 */
	public int indexOf(Student stu); 
     
	/**
	 * 判断数组队列中是否有元素
	 * @return 返回true表示没有元素是空的;返回false表示非空
	 */
	public boolean isEmpty(); 
	
	/**
	 * 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
	 * @param stu要查找的元素
	 * @return 返回元素的索引,如果返回-1表示元素不存在
	 */
	public int lastIndexOf(Student stu); 
}

 

//实现接口的类
public class MyListImpl implements MyList {
	private Student[] array = new Student[0];

	public MyListImpl() {

	}

	/**
	 * 添加新元素的方法
	 * 
	 * @param stu是新元素
	 */
	public void add(Student stu) {
		Student[] temp = new Student[array.length + 1];
		for (int i = 0; i < array.length; i++) {
			temp[i] = array[i];
		}
		temp[array.length] = stu;
		array = temp;
	}

	/**
	 * 在指定位置添加一个新元素
	 * 
	 * @param index指定的索引位置
	 * @param stu要添加的新元素
	 * @return 返回true表示添加成功,返回false表示添加失败
	 */
	public boolean add(int index, Student stu) {
		Student[] temp = new Student[array.length + 1];
		if (index < array.length) {
			for (int i = 0; i < index; i++) {
				temp[i] = array[i];
			}
			temp[index] = stu;
			for (int i = index; i < array.length; i++) {
				temp[i + 1] = array[i];
			}
			array = temp;
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 将数组队列mal中的数据添加到当前数组队列的末尾
	 * 
	 * @param mal要被添加的数组队列
	 */
	public void add(MyList dest) {
		// 是原长加上目标长
		Student[] temp = new Student[array.length + dest.size()];
		// copy src
		for (int i = 0; i < array.length; i++) {
			temp[i] = array[i];
		}

		for (int i = array.length; i < array.length + dest.size(); i++) {
			temp[i] = dest.get(i - array.length);
		}
		array = temp;
	}

	/**
	 * 在指定位置将参数数组队列中的元素添加到该位置
	 * 
	 * @param index指定的索引位置
	 * @param mal要被添加的数组队列
	 * @return 返回true表示添加成功,返回false表示添加失败
	 */
	public boolean add(int index, MyList mal) {
		Student[] temp = new Student[array.length + mal.size()];
		if (index < array.length) {
			for (int i = 0; i < index; i++) {
				temp[i] = array[i];
			}
			for (int i = index, j = 0; i < array.length + mal.size(); i++, j++) {
				temp[i] = mal.get(j);
			}
			for (int i = index + mal.size(), j = index; i < array.length
					+ mal.size(); j++, i++) {
				temp[i] = array[j];
			}
			array = temp;
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 移除指定索引位置的元素
	 * 
	 * @param index指定的索引位置
	 * @return 返回被移除的对象,如果返回Null则表示index的索引不存在
	 */
	public Student remove(int index) {
		Student[] temp = new Student[array.length - 1];
		if (index < array.length) {
			for (int i = 0; i < index; i++) {
				temp[i] = array[i];
			}
			for (int i = index; i < array.length - 1; i++) {
				temp[i] = array[i + 1];
			}
			Student indexdata = array[index];
			array = temp;
			return indexdata;
		} else {
			return null;
		}

	}

	/**
	 * 移除指定的元素对象
	 * 
	 * @param stu要被移除的元素
	 * @return 返回true表示移除成功,返回false表示移除失败
	 */
	public boolean remove(Student stu) {
		int index = -1;
		for (int j = 0; j < array.length; j++) {
			if (array[j].equals(stu)) {
				index = j;
				Student[] temp = new Student[array.length - 1];
				if (index < array.length) {
					for (int i = 0; i < index; i++) {
						temp[i] = array[i];
					}
					for (int i = index + 1; i < array.length; i++) {
						temp[i - 1] = array[i];
					}
					array = temp;

				}
			}
		}

		if (index >= 0 && index < array.length) {
			return true;
		} else {

			return false;
		}
	}

	/**
	 * 移除mal数组队列中的元素
	 * 
	 * @param mal要被移除的数组队列
	 * @return 返回true表示移除成功,返回false表示移除失败
	 */
	public boolean remove(MyList mal) {
		Student[] temp = new Student[array.length - mal.size()];
		int index = -1;
		for (int i = 0; i < array.length; i++) {
			if (array[i].equals(mal.get(i))) {
				index = i;
			}
		}
		for (int j = 0; j < index; j++) {
			temp[j] = array[j];
		}
		for (int j = index; j < array.length - mal.size(); j++) {
			temp[j] = array[j + mal.size()];
		}
		array = temp;
		if (index > 0 && index < array.length) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 修改指定索引位置的元素,用stu来代替
	 * 
	 * @param index指定的索引位置
	 * @param stu要替换的新元素
	 * @return 返回被替换的对象,如果返回Null则表示index的索引不存在
	 */
	public Student set(int index, Student stu) {
		Student[] temp = new Student[array.length];

		if (index < array.length) {
			for (int i = 0; i < index; i++) {
				temp[i] = array[i];
			}
			temp[index] = stu;
			for (int i = index + 1; i < array.length; i++) {
				temp[i] = array[i - 1];
			}
			array = temp;
			return stu;
		} else {
			return null;
		}
	}

	/**
	 * 获取指定索引位置的元素
	 * 
	 * @param index指定的索引位置
	 * @return 返回索引位置的对象,如果返回Null则表示index的索引不存在
	 */
	public Student get(int index) {
		if (index < array.length) {
			return array[index];
		} else {
			return null;
		}
	}

	/**
	 * 返回数组队列中存储的元素总数
	 * 
	 * @return 返回元素总数
	 */
	public int size() {
		return array.length;
	}

	/**
	 * 清除数组队列中所有的元素
	 */
	public void clear() {
		for (int i = 0; i < array.length; i++) {
			array[i] = null;
		}
	}

	/**
	 * 返回数组队列中首次出现的指定元素的索引,或如果此数组队列不包含元素,则返回 -1。
	 * 
	 * @param stu要查找的元素
	 * @return 返回元素的索引,如果返回-1表示元素不存在
	 */
	public int indexOf(Student stu) {
		int index = -1;
		for (int j = 0; j < array.length; j++) {
			if (array[j].equals(stu)) {
				index = j;
				break;
			}
		}
		return index;
	}

	/**
	 * 判断数组队列中是否有元素
	 * 
	 * @return 返回true表示没有元素是空的;返回false表示非空
	 */
	public boolean isEmpty() {
		if (array.length != 0) {
			return true;
		}
		return false;
	}

	/**
	 * 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
	 * 
	 * @param stu要查找的元素
	 * @return 返回元素的索引,如果返回-1表示元素不存在
	 */
	public int lastIndexOf(Student stu) {
		int index = -1;
		for (int j = array.length - 1; j >= 0; j--) {
			if (array[j].equals(stu)) {
				index = j;
				break;
			}
		}
		return index;
	}
}

 

 

 

<!--EndFragment-->

同步定位与地图构建(SLAM)技术为移动机器人或自主载具在未知空间中的导航提供了核心支撑。借助该技术,机器人能够在探索过程中实时构建环境地图并确定自身位置。典型的SLAM流程涵盖传感器数据采集、数据处理、状态估计及地图生成等环节,其核心挑战在于有效处理定位与环境建模中的各类不确定性。 Matlab作为工程计算与数据可视化领域广泛应用的数学软件,具备丰富的内置函数与专用工具箱,尤其适用于算法开发与仿真验证。在SLAM研究方面,Matlab可用于模拟传感器输出、实现定位建图算法,并进行系统性能评估。其仿真环境能显著降低实验成本,加速算法开发与验证周期。 本次“SLAM-基于Matlab的同步定位与建图仿真实践项目”通过Matlab平台完整再现了SLAM的关键流程,包括数据采集、滤波估计、特征提取、数据关联与地图更新等核心模块。该项目不仅呈现了SLAM技术的实际应用场景,更为机器人导航与自主移动领域的研究人员提供了系统的实践参考。 项目涉及的核心技术要点主要包括:传感器模型(如激光雷达与视觉传感器)的建立与应用、特征匹配与数据关联方法、滤波器设计(如扩展卡尔曼滤波与粒子滤波)、图优化框架(如GTSAM与Ceres Solver)以及路径规划与避障策略。通过项目实践,参与者可深入掌握SLAM算法的实现原理,并提升相关算法的设计与调试能力。 该项目同时注重理论向工程实践的转化,为机器人技术领域的学习者提供了宝贵的实操经验。Matlab仿真环境将复杂的技术问题可视化与可操作化,显著降低了学习门槛,提升了学习效率与质量。 实践过程中,学习者将直面SLAM技术在实际应用中遇到的典型问题,包括传感器误差补偿、动态环境下的建图定位挑战以及计算资源优化等。这些问题的解决对推动SLAM技术的产业化应用具有重要价值。 SLAM技术在工业自动化、服务机器人、自动驾驶及无人机等领域的应用前景广阔。掌握该项技术不仅有助于提升个人专业能力,也为相关行业的技术发展提供了重要支撑。随着技术进步与应用场景的持续拓展,SLAM技术的重要性将日益凸显。 本实践项目作为综合性学习资源,为机器人技术领域的专业人员提供了深入研习SLAM技术的实践平台。通过Matlab这一高效工具,参与者能够直观理解SLAM的实现过程,掌握关键算法,并将理论知识系统应用于实际工程问题的解决之中。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值